可视化ValueError Cannot mask with non-boolean array containing NA NaN values

bug:raise ValueError(na_msg)
ValueError: Cannot mask with non-boolean array containing NA / NaN values

image-20230609104001525

对应的代码:

image-20230609104134931

asian_countries = region_data.dropna(subset=['Country Code'])[
    region_data['Region'].str.contains('Asia')]['Country Code']

出错原因:

image-20230609104414748

解决方法:

# 这里使用了fillna()方法将Region列中的空白数据填充为一个空字符串,
# 然后再使用dropna()方法删除Country Code列中的NaN值,最后再进行亚洲国家的筛选。
region_data['Region'].fillna('', inplace=True)
asian_countries = region_data.dropna(subset=['Country Code'])[
    region_data['Region'].str.contains('Asia')]['Country Code']

print(asian_countries)

运行结果:

image-20230609105100830

成功啦!

posted @ 2023-09-20 15:55  Lovi*  阅读(103)  评论(0编辑  收藏  举报