Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joining two unequal dataframes

Tags:

python

pandas

I have two dataframes of unequal length. The first dataframe(df1) has column A with unique values and corresponding to that are column B and column C in the same dataframe. The second dataframe(df2) has column named column A having multiple repetitions of values of Column A of df1 and corresponding to that column D and column E. I want to join df1 to df2 on column A, in such a way that they form one dataframe and the column B and column C has values corresponding to that in df1.

Df1

  column A. Column B

A.                     ab
B.                      bc
C.                      fg

Df2
Column A   column D
A.                      1
B.                       2
C.                       3
B.                        4
A.                        5
A.                         6



Df1 + Df2

Column A column D column B
A.                  1.              ab
B.                  2.               bc
C.                   3.              fg
B.                   4.              bc
A.                   5.              ab 
A.                    6.              ab

How to do that ?

like image 785
vaibhav chawla Avatar asked Oct 21 '25 12:10

vaibhav chawla


1 Answers

You can use merge without parameter on if only common column in both DataFrame is joined column Column A with left join:

print (DF1)
  Column A Column B  Column C
0       A.       ab         1
1       B.       bc         9
2       C.       fg         7

print (DF2)
  Column A  column D  column E
0       A.         1         8
1       B.         2         9
2       C.         3         7
3       B.         4         3
4       A.         5         1
5       A.         6         0

df = DF2.merge(DF1,how='left')
print (df)
  Column A  column D  column E Column B  Column C
0       A.         1         8       ab         1
1       B.         2         9       bc         9
2       C.         3         7       fg         7
3       B.         4         3       bc         9
4       A.         5         1       ab         1
5       A.         6         0       ab         1

If multiple same columns add on:

df = DF2.merge(DF1,how='left', on='Column A')
print (df)
  Column A  column D  column E Column B  Column C
0       A.         1         8       ab         1
1       B.         2         9       bc         9
2       C.         3         7       fg         7
3       B.         4         3       bc         9
4       A.         5         1       ab         1
5       A.         6         0       ab         1
like image 170
jezrael Avatar answered Oct 23 '25 02:10

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!