Search code examples
pythondataframemergeconcatenation

How to mergr/concat two dataframe with different column length?


I have a problem about merging or concatenating of two dataframes with different column length. Below two dataframes are having 3 same columns names and df2 has a another columns.

a = [100, 66, 80]
b = [26, 53, 45]
c = [94, 100, 32]
df1 = pd.DataFrame([a,b,c], columns = ['A', 'C', 'D'])
a = [88, 94, 21, 39]
b = [82, 79, 19, 87]
c = [20, 10, 92, 13]
df2 = pd.DataFrame([a,b,c], columns = ['A', 'B', 'C', 'D'])

What I want to do now is merging two dataframes likes below

Expected

enter image description here

But Output is likes below

Output

enter image description here

I used 'pd.concat' and 'pd.merge' but anyone is not working well.

pd.concat([df1, df2], join = 'outer')
pd.merge(df1, df2, how = 'outer')

Does anyone can give me an advice how I can get the result table?

I'm not a programmer and this is my first time to use 'stackoverlow' so I don't know well how I use this site. So..understand me :^) Thank you for yout helping.


Solution

  • If you are worried only about the order of the columns, you can use the reindex function.

    new_df = pd.merge(df1, df2, how= 'outer')
    new_df = new_df.reindex(columns=['A', 'B', 'C', 'D'])
    

    This will update the order of the columns of your DataFrame.

    Edit: I hadn't mentioned about matching of the data because the expected output and actual output differed only on the order of the columns. If you want to match the rows based on the data in the column, you need to use the on parameter:

    new_df = pd.merge(df1, df2, how='outer', on=['A', 'C'])
    

    The on parameter should specify a column/list of columns whose data should match in both the dataframes in order for the rows to be considered matching.