简体   繁体   中英

find the values that are equal in two columns of pandas dataframe

This is my dataframe:

df = pd.DataFrame({'ma':['a', 'b', 'c', 'd'], 'freq1':[1,2,3,4], 'phd':['x', 'y', 'a','b'], 'freq2':[6,7,8,9]})

I want to choose the part of df that ma and phd have common values(that is a and b ) This is my desired outcome:

  freq1  freq2 ma phd
0      1      8  a   a
1      2      9  b   b

So I think you can do a self merge:

m=df.merge(df,left_on='ma',right_on='phd')
m.loc[:,['ma_x','freq1_x','freq2_y','phd_y']]

  ma_x  freq1_x  freq2_y phd_y
0    a        1        8     a
1    b        2        9     b

Alternatively, split df into two pieces and then merge on "ma" and "phd":

df1 = df[['ma', 'freq1']]
df2 = df[['phd', 'freq2']]
df1.merge(df2, left_on='ma', right_on='phd')

  ma  freq1 phd  freq2
0  a      1   a      8
1  b      2   b      9

just use pd.merge()

final_res = pd.merge(df[['ma','freq1']],df[['phd','freq2']],left_on=['ma'],right_on=['phd'],how='inner')
print(final_res)
    ma  freq1  phd  freq2
0   a    1      a   8
1   b    2      b   9

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