👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
The simple task of adding a row to a pandas.DataFrame
object seems to be hard to accomplish. There are 3 stackoverflow questions relating to this, none of which give a working answer.
Here is what I"m trying to do. I have a DataFrame of which I already know the shape as well as the names of the rows and columns.
>>> df = pandas.DataFrame(columns=["a","b","c","d"], index=["x","y","z"])
>>> df
a b c d
x NaN NaN NaN NaN
y NaN NaN NaN NaN
z NaN NaN NaN NaN
Now, I have a function to compute the values of the rows iteratively. How can I fill in one of the rows with either a dictionary or a pandas.Series
? Here are various attempts that have failed:
>>> y = {"a":1, "b":5, "c":2, "d":3}
>>> df["y"] = y
AssertionError: Length of values does not match length of index
Apparently it tried to add a column instead of a row.
>>> y = {"a":1, "b":5, "c":2, "d":3}
>>> df.join(y)
AttributeError: "builtin_function_or_method" object has no attribute "is_unique"
Very uninformative error message.
>>> y = {"a":1, "b":5, "c":2, "d":3}
>>> df.set_value(index="y", value=y)
TypeError: set_value() takes exactly 4 arguments (3 given)
Apparently that is only for setting individual values in the dataframe.
>>> y = {"a":1, "b":5, "c":2, "d":3}
>>> df.append(y)
Exception: Can only append a Series if ignore_index=True
Well, I don"t want to ignore the index, otherwise here is the result:
>>> df.append(y, ignore_index=True)
a b c d
0 NaN NaN NaN NaN
1 NaN NaN NaN NaN
2 NaN NaN NaN NaN
3 1 5 2 3
It did align the column names with the values, but lost the row labels.
>>> y = {"a":1, "b":5, "c":2, "d":3}
>>> df.ix["y"] = y
>>> df
a b
x NaN NaN
y {"a": 1, "c": 2, "b": 5, "d": 3} {"a": 1, "c": 2, "b": 5, "d": 3}
z NaN NaN
c d
x NaN NaN
y {"a": 1, "c": 2, "b": 5, "d": 3} {"a": 1, "c": 2, "b": 5, "d": 3}
z NaN NaN
That also failed miserably.
So how do you do it ?
👻 Read also: what is the best laptop for engineering students?
We hope this article has helped you to resolve the problem. Apart from Python pandas: fill a dataframe row by row, check other code Python module-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano Python pandas: fill a dataframe row by row
- Deutsch Python pandas: fill a dataframe row by row
- Français Python pandas: fill a dataframe row by row
- Español Python pandas: fill a dataframe row by row
- Türk Python pandas: fill a dataframe row by row
- Русский Python pandas: fill a dataframe row by row
- Português Python pandas: fill a dataframe row by row
- Polski Python pandas: fill a dataframe row by row
- Nederlandse Python pandas: fill a dataframe row by row
- 中文 Python pandas: fill a dataframe row by row
- 한국어 Python pandas: fill a dataframe row by row
- 日本語 Python pandas: fill a dataframe row by row
- हिन्दी Python pandas: fill a dataframe row by row
Warsaw | 2023-04-01
os Python module is always a bit confusing 😭 Python pandas: fill a dataframe row by row is not the only problem I encountered. I am just not quite sure it is the best method
Massachussetts | 2023-04-01
Maybe there are another answers? What Python pandas: fill a dataframe row by row exactly means?. Checked yesterday, it works!
Rome | 2023-04-01
I was preparing for my coding interview, thanks for clarifying this - Python pandas: fill a dataframe row by row in Python is not the simplest one. Will get back tomorrow with feedback