如何在np.where() 中检查一行数据是否在列表中?

How to check whether data of a row is in list, inside of np.where()?

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()."

recent_indicators 是 pandas DataFrame。有什么方法可以检查 developed_countries 中是否提到了 'CountryCode'?

可以直接在pandas过滤中使用.isin() -

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

另外,你可以想出一个布尔值列,如果开发了 True -

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