dchoi
dchoi

Reputation: 35

Appending a list to a dataframe

I have a dataframe let's say:

col1 col2 col3
1    x    3
1    y    4

and I have a list:

2
3
4
5

Can I append the list to the data frame like this:

col1 col2 col3
1    x    3
1    y    4
2    Nan  Nan
3    Nan  Nan
4    Nan  Nan
5    Nan  Nan

Thank you.

Upvotes: 1

Views: 4601

Answers (1)

jezrael
jezrael

Reputation: 862406

Use concat or append with DataFrame contructor:

df = df.append(pd.DataFrame([2,3,4,5], columns=['col1']))

df = pd.concat([df, pd.DataFrame([2,3,4,5], columns=['col1'])])


print (df)
   col1 col2  col3
0     1    x   3.0
1     1    y   4.0
0     2  NaN   NaN
1     3  NaN   NaN
2     4  NaN   NaN
3     5  NaN   NaN

Upvotes: 2

Related Questions