【问题标题】:Find a value from list based on condition根据条件从列表中查找值
【发布时间】:2021-06-22 07:58:11
【问题描述】:

我有两个数据集 data1 和 data2
在 data1 列中是 lat long locationid1 和 countrycode
在 data2 列中是 lat long locationid2。
我写了类似
的代码 if locationid2!=None and len(locationid2)>0 and str(locationid2)!='nan' and locationid2 in list(data["locationid1"]):
macth={真}

但是在这里我想在matching.like中添加国家代码..如果locationid1具有coluntrycode =“GB”那么只有它与locationid2匹配 数据1

预期输出

【问题讨论】:

  • 您能否编辑您的问题并在那里发布小输入数据框和预期输出? (格式正确)

标签: python python-3.x list dataframe


【解决方案1】:

试试:

ids = data1.loc[data1["countrycode"].eq("GB"), "locationid1"]
data2["match"] = np.where(data2["locationid2"].isin(ids), "TRUE", "FALSE")
print(data2)

打印:

   lat  long  locationid2  match
0    6    42          111  FALSE
1    7    43          112  FALSE
2    8    44          113   TRUE
3    9    45          114   TRUE
4   10    46          115  FALSE

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 2023-01-26
    • 1970-01-01
    • 1970-01-01
    • 2021-11-16
    • 1970-01-01
    • 2011-09-11
    相关资源
    最近更新 更多