Search code examples
pythonpandasdataframerows

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.


Solution

  • 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