3

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.

2
  • AFAIK it's not possible, because all columns must have the same length (same number of rows) Commented May 5, 2016 at 17:39
  • Ah no. That means I'll have to make separate dataframes or something for my longer data.... thanks hey. Commented May 5, 2016 at 17:45

1 Answer 1

6

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.