简体   繁体   中英

Compare values in two dataframes

I have two dataframes. When values in data1['col1'] match with values in data2['col1'], I want to apply a string change in data1['col2']. I don't have any error, but the change is not applying.

Sample of the input:

data1 = {"col1": ["001", "002", "004"], "col2" : ["SL", "SL", "SL"]}
df1 = pd.DataFrame(data1)
data2  = {"col1": ["009", "004", "025"]}
df2 = pd.DataFrame(data2)

print(df1)
print(df2)

col1 col2
0  001   SL
1  002   SL
2  004   SL

col1
0  009
1  004
2  025

for idx, val in df1["col1"].iteritems():
    for idx2, val2 in df2["col1"].iteritems():
        if val == val2:
            df1["col2"][idx] = "SL MATCH"

Output expected:

print(df1)

col1      col2
0  001        SL
1  002        SL
2  004  SL MATCH

Use Series.isin for check values by another column:

df1.loc[df1['col1'].isin(df2['col1']), "col2"] = "SL MATCH"
print (df1)
  col1      col2
0  001        SL
1  002        SL
2  004  SL MATCH

Or:

df1["col2"] = np.where(df1['col1'].isin(df2['col1']), "SL MATCH", df1["col2"])

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