Python pandas: populate data row by row

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?

+59
python pandas dataframe row
Jun 13 '13 at 16:02
source share
4 answers

df['y'] set the column

since you want to set the string use .loc

Note that .ix is equivalent here, your setbacks, because you were trying to assign a dictionary to each element of the y string is probably not what you want; converting to a series tells pandas that you want to align the input (for example, you don't need to specify all the elements)

 In [7]: df = pandas.DataFrame(columns=['a','b','c','d'], index=['x','y','z']) In [8]: df.loc['y'] = pandas.Series({'a':1, 'b':5, 'c':2, 'd':3}) In [9]: df Out[9]: abcd x NaN NaN NaN NaN y 1 5 2 3 z NaN NaN NaN NaN 
+50
Jun 13 '13 at 16:19
source share
β€” -

My approach was, but I can not guarantee that this is the fastest solution.

 df = pd.Dataframe(columns=["firstname", "lastname"]) df = df.append({ "firstname": "John", "lastname": "Johny" }, ignore_index=True) 
+18
Mar 16 '17 at 15:00
source share

This is a simpler version.

 df = DataFrame(columns=('col1', 'col2', 'col3')) for i in range(5): df.loc[i] = ['<some value for first>','<some value for second>','<some value for third>']` 
+9
Nov 09 '16 at 7:25
source share

If your input strings are lists, not dictionaries, then this is a simple solution:

 import pandas as pd list_of_lists = [] list_of_lists.append([1,2,3]) list_of_lists.append([4,5,6]) pd.DataFrame(list_of_lists, columns=['A', 'B', 'C']) # ABC # 0 1 2 3 # 1 4 5 6 
+2
Aug 03 '17 at 21:46 on
source share



All Articles