0

I have 2 dataframes with different number of rows. However both pandas dataframes share a common column. I want to join the 2 dataframes with rows duplicated based on shared column. Sample dataframes are given below.

#df1 is as below
print(df1)
#output
Station Id  variable1  variable2  variable3  variable 4  variable5
0        1189          200       506       144          0          0
1        1189          300       10      1000          0          0
2        1189          400      504       46          0          0
3         958           0       0       80         20          0
4         958           0       0       14         86          0

print(df2) produces below output.

Station Id  barrenland  forest  priarie 
0        1189           0      56       44     
1        958            0       0      100          

Now, I am looking for joining df2 with df1. The output should contain all rows from df1 and the column values of df2 i.e., barrenland, forest and priarie should repeat in output based on the station Id.

I have tried the following line of code with no success.

output = df1.join(df2,'Station Id')
srinivas
  • 301
  • 1
  • 9

0 Answers0