Home > Software engineering >  Joining data frames one after the other with no matching columns - Python pandas
Joining data frames one after the other with no matching columns - Python pandas

Time:11-01

I have two dataframes like:

A B
1 2
3 4
5 6

df2:

C D
7 8
9 10
2 3
4 5

I wish to combine these dataframes one after the other like the following:

A B C D
1 2 
3 4 
5 6 
    7 8 
    9 10
    2 3
    4 5

I have tried using concat and join but it doesnt give me this desired output.

Thanks

CodePudding user response:

import pandas as pd
import numpy as np


df1 = pd.DataFrame({
    'A' : [1, 3, 5],
    'B' : [2, 4, 6]
})

df2 = pd.DataFrame({
    'C' : [7, 9, 2, 4],
    'D' : [8, 10, 3, 5]
})


def foo(df1, df2):
    df_1 = df1.copy()
    df_2 = df2.copy()
    df1[df_2.columns] = np.nan
    df2[df_1.columns] = np.nan

    df = pd.concat([df1, df2])
    df.reset_index(drop = True, inplace = True)
    
    return df


if __name__ == '__main__':
    print(foo(df1, df2))

Output :

     A    B    C     D
0  1.0  2.0  NaN   NaN
1  3.0  4.0  NaN   NaN
2  5.0  6.0  NaN   NaN
3  NaN  NaN  7.0   8.0
4  NaN  NaN  9.0  10.0
5  NaN  NaN  2.0   3.0
6  NaN  NaN  4.0   5.0

If you want blank, rather than NAN, then edit the code like below.

    df1[df_2.columns] = ''
    df2[df_1.columns] = ''

Output :

   A  B  C   D
0  1  2
1  3  4
2  5  6
3        7   8
4        9  10
5        2   3
6        4   5

CodePudding user response:

import pandas as pd
import numpy as np

x = pd.DataFrame(np.random.randint(6,size = (3,2)),columns = ['A','B'])
y = pd.DataFrame(np.random.randint(6,size = (3,2)),columns = ['C','D'])
z = pd.concat([x,y], axis = 0, ignore_index=True)
z.replace(np.NaN,'', inplace=True)
print(x)
print(y)
print(z)

Output:

   A  B
0  2  2
1  3  0
2  2  3
   C  D
0  4  1
1  4  4
2  4  4
     A    B    C    D
0  2.0  2.0          
1  3.0  0.0          
2  2.0  3.0          
3            4.0  1.0
4            4.0  4.0
5            4.0  4.0
  • Related