简体   繁体   中英

Putting values in a column in pandas dataframe from another dataframe based on multiple condition check

I have two dataframes df1 and df2 . I want to put a column new_id in df1 with values from df2 .

s = {'id': [4735,46,2345,8768,807,7896],
     'st': ['a', 'a', 'd', 'e', 'f', 'a'], 
     'rd' : ['CU', 'SU', 'NU', 'NU', 'W', 'CU'], 
     'cm' : ['m', 'm', 'm', 'm', 'm','m']}
df1 = pd.DataFrame(s)

df1

     id st  rd cm
0  4735  a  CU  m
1    46  a  SU  m
2  2345  d  NU  m
3  8768  e  NU  m
4   807  f   W  m
5  7896  a  CU  m


s2 = {'id': [1234,4567,1357,2367,8765, 8796, 5687, 4565, 7865],
     'st': ['a', 'a', 'd', 'd', 'f', 'f','e' ,'e','a'], 
     'rd' : ['CU', 'SU', 'NU', 'W', 'W','NU','W','CU','W'], 
     'cm' : ['s', 's', 's', 's', 's','s','s','s','s']}
df2 = pd.DataFrame(s2)

df2

     id st  rd cm
0  1234  a  CU  s
1  4567  a  SU  s
2  1357  d  NU  s
3  2367  d   W  s
4  8765  f   W  s
5  8796  f  NU  s
6  5687  e   W  s
7  4565  e  CU  s
8  7865  a   W  s

I wanted the values in new_id to be put in df1 from id column of df2 where the st value should be same and rd value should be different.

and once a value is picked from df2 that value should not be used again. How can I do it in pandas

I am expecting the result:

     id st  rd cm  new_id
0  4735  a  CU  m  4567
1    46  a  SU  m  1234
2  2345  d  NU  m  2367
3  8768  e  NU  m  5687
4   807  f   W  m  8796
5  7896  a  CU  m  7865

Use np.equal.outer comparison to get this cross-data-frame match, and np.argmax to retrieve the indexes.

comp = np.equal.outer(df1.st, df2.st) & ~np.equal.outer(df1.rd, df2.rd)
df1['new_id'] = df2.id[np.argmax(comp, axis=1)].tolist()

    id      st  rd  cm  new_id
0   4735    a   CU  m   4567
1   46      a   SU  m   1234
2   2345    d   NU  m   2367
3   8768    e   NU  m   5687
4   807     f   W   m   8796

How about this?

df3 = df2.copy()

def cond(row):
    cond = ((df3['st'] == row['st']) & (df3['rd'] != row['rd']))
    tmp = df3.loc[cond, 'id']
    val = tmp.iloc[0]
    idx = tmp[tmp == val].index[0]
    df3.drop(idx, inplace=True)
    return val

df1.assign(new_id=df1.apply(cond, axis=1))

     id st  rd cm  new_id
0  4735  a  CU  m    4567
1    46  a  SU  m    1234
2  2345  d  NU  m    2367
3  8768  e  NU  m    5687
4   807  f   W  m    8796
5  7896  a  CU  m    7865

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