Home > other >  Concatenating two dataframes with no common columns but same row dimension
Concatenating two dataframes with no common columns but same row dimension

Time:07-11

I have two dataframes df1 (dimension: 2x3) and df2 (dimension: 2x239) taken for example - each having the same number of rows but a different number of columns.

I need to concatenate them to get a new dataframe df3 (dimension 2x242).

I used the concat function but ended up getting a (4x242) which has NaN values.

Need help.

Screenshot attached. jupyter notebook screenshot

CodePudding user response:

If you want to add these new columns in the same row. Try the below code : '''' pd.concat([df1,df2],axis=1) '''' The output shape will be (2*total_cols)

CodePudding user response:

You need to set the axis=1 parameter

pd.concat([df1.reset_index(), df2.reset_index()], axis=1)

CodePudding user response:

You need concat on columns, from your pic, df2 index is duplicated. You need reset it to normal with .reset_index(drop=True)

out = pd.concat([df1.reset_index(drop=True), df2.reset_index(drop=True)], axis=1)
  • Related