Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending a list or series to a pandas DataFrame as a row?

So I have initialized an empty pandas DataFrame and I would like to iteratively append lists (or Series) as rows in this DataFrame. What is the best way of doing this?

like image 279
Wes Field Avatar asked Oct 20 '22 00:10

Wes Field


2 Answers

Sometimes it's easier to do all the appending outside of pandas, then, just create the DataFrame in one shot.

>>> import pandas as pd
>>> simple_list=[['a','b']]
>>> simple_list.append(['e','f'])
>>> df=pd.DataFrame(simple_list,columns=['col1','col2'])
   col1 col2
0    a    b
1    e    f
like image 170
Mike Chirico Avatar answered Oct 21 '22 14:10

Mike Chirico


df = pd.DataFrame(columns=list("ABC"))
df.loc[len(df)] = [1,2,3]
like image 170
Ashot Matevosyan Avatar answered Oct 21 '22 14:10

Ashot Matevosyan