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

时间:2018-02-05 11:35:27

标签: python pandas dataframe np

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:系列的真值是不明确的。使用a.empty,   a.bool(),a.item(),a.any()或a.all()。"

recent_indicators 是pandas DataFrame。什么可以替代检查' CountryCode'在develop_countries中提到了什么?

1 个答案:

答案 0 :(得分:2)

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

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

此外,您可以提出一个布尔列,如果已开发,则显示True -

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