Concatenate two dataframes with different number of rows and columns

Clash Royale CLAN TAG#URR8PPP
Concatenate two dataframes with different number of rows and columns
I have two dataframes:
df1 shape = (101, 4825)
df2 shape = (97, 5818)
The first 4825 column names of df2 are the same as df1, and then increases by +1.
df2
df1
However, at the end of both dataframes, there is a column named Group_number.
Group_number
I want to concatenate both the data frames so that the shape of the final dataframe is of shape (198,5818), i.e the final dataframe has all the rows of both the and NaN values for the df1 section (after the initial 4825 values).
NaN
df1
I tried pd.concat([df1,df2]) but the column Group_number gets mixed up.
pd.concat([df1,df2])
Group_number
1 Answer
1
This could happening because of index problem as well. Use arg "ignore_index":
pd.concat([df1,df2], ignore_index=True)
or you can test by using "keys" argument so that you will know which observation is of which original data frame:
pd.concat([df1,df2], ignore_index=True, keys=['a', 'b'])
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.