The simple task of adding a row to a pandas.DataFrame object seems like a daunting task. There are three stack questions associated with this, none of which give a working answer.
Here is what I am trying to do. I have a DataFrame from which I already know the form, as well as the names of the rows and columns.
>>> df = pandas.DataFrame(columns=['a','b','c','d'], index=['x','y','z']) >>> df abcd x NaN NaN NaN NaN y NaN NaN NaN NaN z NaN NaN NaN NaN
Now I have a function to compute the string values ββiteratively. How can I populate one of the lines with either a dictionary or pandas.Series ? Here are some unsuccessful attempts:
>>> y = {'a':1, 'b':5, 'c':2, 'd':3} >>> df['y'] = y AssertionError: Length of values does not match length of index
Apparently, he 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, this is only for setting individual values ββin the data frame.
>>> y = {'a':1, 'b':5, 'c':2, 'd':3} >>> df.append(y) Exception: Can only append a Series if ignore_index=True
Well, I do not want to ignore the index, otherwise here is the result:
>>> df.append(y, ignore_index=True) abcd 0 NaN NaN NaN NaN 1 NaN NaN NaN NaN 2 NaN NaN NaN NaN 3 1 5 2 3
He matched column names with values, but lost row labels.
>>> y = {'a':1, 'b':5, 'c':2, 'd':3} >>> df.ix['y'] = y >>> df ab \ x NaN NaN y {'a': 1, 'c': 2, 'b': 5, 'd': 3} {'a': 1, 'c': 2, 'b': 5, 'd': 3} z NaN NaN cd x NaN NaN y {'a': 1, 'c': 2, 'b': 5, 'd': 3} {'a': 1, 'c': 2, 'b': 5, 'd': 3} z NaN NaN
It also failed.
So how do you do this?