如何检查一行的数据是否在列表中,在np.哪里()?

2024-05-19 19:29:24 发布

您现在位置:Python中文网/ 问答频道 /正文

developed_countries = ["NOR","AUS","CHE","DEU","DNK","SGP","NLD","IRL","ISL","CAN","USA","NZL","SWE","LIE","GBR"]

recent_indicators['Developed'] = np.where(recent_indicators['CountryCode'] in developed_countries, 1, 0)

"ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()."

最近的\u指标是数据帧。有什么办法可以检查发达国家是否提到“国家代码”?你知道吗


Tags: chesgpcountriescanrecentnorirlusa
1条回答
网友
1楼 · 发布于 2024-05-19 19:29:24

您可以直接在中使用.isin()-

recent_indicators_filtered = recent_indicators[recent_indicators['CountryCode'].isin(developed_countries)]

此外,如果开发了一个布尔列,它会显示True-

recent_indicators['Developed'] = recent_indicators['CountryCode'].isin(developed_countries)

相关问题 更多 >