'python Pandas append multiple dataframe [closed]
I've read many solutions on my question but any of them work for me. I've a first df like:
col1 | col2 | col4 |
---|---|---|
a1 | b1 | d1 |
A 2nd df like:
col2 | col3 | col5 |
---|---|---|
b2 | c2 | e2 |
and I want :
col1 | col2 | col3 | col4 | col5 |
---|---|---|---|---|
a1 | b1 | d1 | ||
b2 | c2 | e2 |
Every solution I read did not work for me.
SOLUTION: I used
df = pandas.concat([df1, df2])
my error was due to post-processing
Solution 1:[1]
You should be able to achieve this with pd.concat
and using the sort argument.
>>> df1 = pd.DataFrame.from_dict({'col1':['a1'],'col2':['b1'],'col4':['d1']})
>>> df2 = pd.DataFrame.from_dict({'col2':['b2'],'col3':['c2'],'col5':['e2']})
>>> pd.concat([df1, df2], sort=True)
col1 col2 col3 col4 col5
0 a1 b1 NaN d1 NaN
0 NaN b2 c2 NaN e2
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Sparrow0hawk |