Python pandas: fill a dataframe row by row Python pandas: fill a dataframe row by row python python

Python pandas: fill a dataframe row by row


df['y'] will set a column

since you want to set a row, use .loc

Note that .ix is equivalent here, yours failed because you tried to assign a dictionaryto each element of the row y probably not what you want; converting to a Series tells pandasthat you want to align the input (for example you then don't have to to specify all of the elements)

In [6]: import pandas as pdIn [7]: df = pd.DataFrame(columns=['a','b','c','d'], index=['x','y','z'])In [8]: df.loc['y'] = pd.Series({'a':1, 'b':5, 'c':2, 'd':3})In [9]: dfOut[9]:      a    b    c    dx  NaN  NaN  NaN  NaNy    1    5    2    3z  NaN  NaN  NaN  NaN


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

df = pd.DataFrame(columns=["firstname", "lastname"])df = df.append({     "firstname": "John",     "lastname":  "Johny"      }, ignore_index=True)


This is a simpler version

import pandas as pddf = pd.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>']`