Python Forum
merge two dataframes with different number of rows
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
merge two dataframes with different number of rows
#1
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
Reply
#2
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
dawid294 likes this post
Reply
#3
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)
Reply
#4
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')
Reply
#5
(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
Reply
#6
Alternatively one can just join two dataframes df_1.join(df_2) or specific column df_1.join(df_2['Pay'])
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sequential number for rows retrieved and storing the Primary UKey to the line number GYKR 2 555 Aug-22-2023, 10:14 AM
Last Post: GYKR
  Check DataFrames with different sorting in columns and rows Foxyskippy 0 753 Nov-19-2022, 07:49 AM
Last Post: Foxyskippy
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,603 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  Pandas DataFrame combine rows by column value, where Date Rows are NULL rhat398 0 2,084 May-04-2021, 10:51 PM
Last Post: rhat398
  Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python Jeremy7 8 6,963 Mar-02-2021, 01:54 AM
Last Post: Jeremy7
  merge dataframes imatiasmb 1 1,433 Oct-17-2020, 06:44 PM
Last Post: klllmmm
  How to merge three DataFrames based on specific column Mekala 0 1,767 Sep-08-2020, 02:01 PM
Last Post: Mekala
  Merge 2 dataframes but avoid double summing of rows? vinaysalian17 0 1,819 Jun-03-2020, 01:48 AM
Last Post: vinaysalian17
  Mysql returning number of rows not data AkaAndrew123 4 2,758 Jun-10-2019, 02:31 PM
Last Post: AkaAndrew123
  Merge rows of 2 arrays vipinv23 2 1,926 Jan-27-2019, 05:45 PM
Last Post: aakashjha001

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020