Python Forum
merge two dataframes with different number of rows - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: merge two dataframes with different number of rows (/thread-32365.html)



merge two dataframes with different number of rows - dawid294 - Feb-05-2021

Hello , please can you help me with merge. i tried find anything in forum but i did not find it.
i have two dataframes and i would like to merge them. df1 and df2 have the same number of rows.

car bus
0 1 8
1 9 5
2 1 8
3 2 8
4 3 8
5 6 5

pay
300 1
345 2
854 8
12 9
69 10

I would like to this
car bus pay
0 1 8 1
1 9 5 2
2 1 8 8
3 2 8 9
4 3 8 10
5 6 5 NA

i tried
df= pd.concat([df1,df2], axis=1)
df=do.call("merge", c(lapply(list(df1, df2), data.frame, row.names=NULL), by = 0, all = TRUE))[-1]

df=pd.merge(df1,df2,by = 0, all = TRUE)[-1]

but it did not work. Can you help me .thank you


RE: merge two dataframes with different number of rows - eddywinch82 - Feb-05-2021

Hi dawid294,

Could you post your entire Python Code, in Python Tags ? if that is okay ?

And I will try to sort out, what you wan't doing, this afternoon.

Regards

Eddie Winch


RE: merge two dataframes with different number of rows - dawid294 - Feb-05-2021

data1
car bus
0 1 8
1 9 5
2 1 8
3 2 8
4 3 8
5 6 5

data2
pay
300 1
345 2
854 8
12 9
69 10

data1=pd.dataframe(data1)
data2=pd.dataframe(data2)
data= pd.concat([data1,data2],axis='1')
print(data)


RE: merge two dataframes with different number of rows - dawid294 - Feb-05-2021

I have already found a solution. thank you for your willingness. you can close the thread.

this is solution:

data1.index=data2.index

and after i can use

data= pd.concat([data1,data2],axis='1')


RE: merge two dataframes with different number of rows - eddywinch82 - Feb-05-2021

(Feb-05-2021, 02:33 PM)dawid294 Wrote: I have already found a solution. thank you for your willingness. you can close the thread.

this is solution:

data1.index=data2.index

and after i can use

data= pd.concat([data1,data2],axis='1')

Great dawid294,

I am pleased to hear, that you have come up with a solution. ))

Regards

Eddie Winch


RE: merge two dataframes with different number of rows - perfringo - Feb-05-2021

Alternatively one can just join two dataframes df_1.join(df_2) or specific column df_1.join(df_2['Pay'])