如何找到与其他数据帧匹配的python数据帧值

时间:2017-07-05 10:29:59

标签: python pandas dataframe

如果df1 ['Field1']值存在于其他数据框df2 ['SersName']

中,我想在df1 ['Result']中更新为True或False

请帮忙......

DF1:

YourUILabel.attributedText   = customString("300", oldPrice: "400")

DF2:

Field1          Field2  Result
2020RATIO001001 A       TRUE
2020RATIO001003 B       TRUE
2020RATIO001005 C       TRUE
2020RATIO001XYZ D       FALSE
2020RATIO001123 E       FALSE

我尝试了以下脚本:

SersName        Field2
2020RATIO001001 1
2020RATIO001003 2
2020RATIO001005 3
2020RATIO001007 4
2020RATIO001009 5
2020RATIO001011 6
2020RATIO001013 7
2020RATIO001015 8

1 个答案:

答案 0 :(得分:0)

您需要使用isin

import pandas as pd

df1 = pd.read_csv(StringIO("""Field1 Field2
2020RATIO001001 A
2020RATIO001003 B
2020RATIO001005 C
2020RATIO001XYZ D
2020RATIO001123 E"""),sep=" ")

df2 = pd.read_csv(StringIO("""SersName Field2
2020RATIO001001 1
2020RATIO001003 2
2020RATIO001005 3
2020RATIO001007 4
2020RATIO001009 5
2020RATIO001011 6
2020RATIO001013 7
2020RATIO001015 8"""),sep=" ")

df1['Result'] = df1['Field1'].isin(df2['SersName'])

结果df1是:

Field1  Field2  Result
0   2020RATIO001001 A   True
1   2020RATIO001003 B   True
2   2020RATIO001005 C   True
3   2020RATIO001XYZ D   False
4   2020RATIO001123 E   False