简体   繁体   中英

joining two panda dataframes

I am having trouble merging these two dataframes to give the expected results. I would like to check both dataframes and if column 1 and 3 of dataframe 1 match column 0 and 2 of dataframe 2 then return the last dataframe.

df1                 
0   1       2        3       4    5
4   4133    43  192.168.0.1 dt1 default
5   4134    43  192.168.0.2 dt2 t2
6   4135    43  192.168.0.3 dt3 t3


df2                 
0       1        2       3     4         5
4134    43  192.168.0.2 dt2 t2  
4136    43  192.168.0.4 dt4 default 
4137    43  192.168.0.5 dt5 default 



result                  
0    1       2       3       4   5
5   4134    43  192.168.0.2 dt2 t2

You could try this using an inner join with merge :

df1[['1','3']].merge(df2, left_on=['1','3'], right_on=['0','2'], suffixes=('_x',''))\
              .drop(['1_x','3_x'], axis=1)

尝试这个:

result = df1.merge(df2, left_on=['1', '3'], right_on=['0', '2']) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM