I am trying to concatenate two Dataframes but an error occurs systematically and I don't know how to correct it.
I saw an example of what I want to do on the Pandas documentation :
However, I am trying to do the same on my two dataframe :
df1 :
df2 :
Some columns are similar but some of them are different. I used reset_index(drop=True)
in order to avoid this problem even if the index of each dataframe are different.
I am trying to do this : pd.concat([df1, df2], ignore_index=True, sort=False)
but I have this error :
Can somebody have an idea of how I could solve this error ? Thanks in advance for your time !
CodePudding user response:
Reset the indices by hand, transpose the dataframes, do an outer join, then transpose them back.
import pandas as pd
df1 = pd.DataFrame({'A': [1,2,3,4,5], 'B':[2,3,4,5,6], 'C':[-9,-2,-1,8,0]})
df2 = pd.DataFrame({'A': [4,5], 'B':[2,6], 'D':[9,0]})
df1.index = range(df1.shape[0])
df2.index = range(df1.shape[0], df1.shape[0] df2.shape[0])
result = df1.T.join(df2.T, how='outer').T
result
A B C D
0 1.0 2.0 -9.0 NaN
1 2.0 3.0 -2.0 NaN
2 3.0 4.0 -1.0 NaN
3 4.0 5.0 8.0 NaN
4 5.0 6.0 0.0 NaN
5 4.0 2.0 NaN 9.0
6 5.0 6.0 NaN 0.0