KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
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'
Tags (comma-separated)
Save Edits
Cancel