简体   繁体   中英

Adding a new column to a pandas dataframe with different number of rows

I'm not sure if pandas is made to do this... But I'd like to add a new row to my dataframe with more rows than the existing columns.

Minimal example:

import pandas as pd
df = pd.DataFrame()
df ['a'] = [0,1]
df ['b'] = [0,1,2]

Could someone please explain if this is possible? I'm using a dataframe to store long lists of data and they all have different lengths that I don't necessarily know at the start.

Absolutely possible. Use pd.concat

Demonstration

df1 = pd.DataFrame([[1, 2, 3]])
df2 = pd.DataFrame([[4, 5, 6, 7, 8, 9]])

pd.concat([df1, df2])

df1 looks like

   0  1  2
0  1  2  3

df2 looks like

   0  1  2  3  4  5
0  4  5  6  7  8  9

pd.concat looks like

   0  1  2    3    4    5
0  1  2  3  NaN  NaN  NaN
0  4  5  6  7.0  8.0  9.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM