我想过滤熊猫数据框中的某些分类值,但会产生错误。这是数据框:
import pandas as pd
s = pd.DataFrame(columns=["Name","Age","id", "Nationality"])
s.loc[0] = ["John",23, "123456789", "Netherlands"]
s.loc[1] = ["Emilia", 29, "456578394", "Italy"]
s.loc[2] = ["Jason",41, "3E567657", "Spain"]
s.loc[3] = ["Esther", 31, "11983734", "Italy"]
s.loc[4] = ["Xu",32, "22385749I04", "China"]
s.loc[5] = ["Robert", 19, "09874849", "Italy"]
s.loc[6] = ["Anna",29, "09654373839", "Netherlands"]
s.loc[7] = ["Maria", 35, "3283947", "Italy"]
s.loc[8] = ["Selena",21, "85950505", "Spain"]
s.loc[9] = ["David", 18, "648549595", "Germany"]
s.loc[10] = ["Jolanda", 28, "9859575", "Netherlands"]
我想用“中国”或“荷兰”的“国籍”过滤所有行。为此,我使用以下代码:
s[s["Nationality"]=="China" or s["Nationality"]=="Spain"]
和python产生此错误:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
如果我只想过滤一个国籍(例如,所有带有“中国”的“国籍”的行),则类似的代码(s[s["Nationality"]=="China")
有效,但是对于多个“国籍”而言,它无效!任何可以解决我的问题的建议,将不胜感激!