DrTchocky
DrTchocky

Reputation: 535

pandas concat adding as columns with nans?

I have two dataframes, each with the same number of columns :

print(df1.shape)
(54, 35238)

print(df2.shape)
(64, 35238)

And both don't have any index set

print(df1.index.name)
None

print(df2.index.name)
None

However, whenever I try to vertically concat them (so to have a third dataframe with shape (118, 35238)), it produces a new df with NaNs:


df3 = pandas.concat([df1, df2], ignore_index=True)
print(df3)

df3 print result

The resultant df has the correct number of rows, but it has decided to concat them as new columns. Using the "axis" flag set to 1 results in the same number of (inappropriate) columns (e.g. shape of (63, 70476)).

Any ideas on how to fix this?

Upvotes: 0

Views: 1094

Answers (2)

zlin
zlin

Reputation: 1

This might be because your df2 is a series, you can try: pd.concat([df1, pd.DataFrame([df2])], axis=0, ignore_index=True)

Upvotes: 0

Dave Costa
Dave Costa

Reputation: 48121

They have the same number of columns, but are the column names different? The documentation on concat suggests to me that you need identical column names to have them stack the way you want.

If this is the problem, you could probably fix it by changing one dataframe's column names to match the other before concatenating:

df2.columns = df1.columns

Upvotes: 1

Related Questions