Let's say I have these two dataframes:
>>> import pandas as pd
>>> df1 = pd.DataFrame({"key":[1,3,5], "columnA":[30,40,50]})
>>> df1
key columnA
0 1 30
1 3 40
2 5 50
>>> df2 = pd.DataFrame({"key":[2,4], "columnB":[60,70]})
>>> df2
key columnB
0 2 60
1 4 70
I basically want a new dataframe, with "key", "columnA", and "columnB", where the corresponding data is "interleaved" from the two above dataframes, correspondingly. I did this:
>>> pd.merge(df1, df2, on='key', how='outer').astype('Int64')
key columnA columnB
0 1 30 <NA>
1 3 40 <NA>
2 5 50 <NA>
3 2 <NA> 60
4 4 <NA> 70
... which comes close - but I want the output to be:
key columnA columnB
0 1 30 <NA>
1 2 <NA> 60
2 3 40 <NA>
3 4 <NA> 70
4 5 50 <NA>
How can I achieve that?