我的数据框boroughCounts
具有以下示例值:
From To Count
9 None Manhattan 302
10 Bronx Bronx 51
11 Bronx Manhattan 244
12 None Brooklyn 8
13 Bronx Queens 100
14 None None 67
按照here或here所述的方法,尝试过滤“发件人”和“收件人”列中的None
值:
boroughCounts = boroughCounts[(boroughCounts.From != None) & (boroughCounts.To != None)]
boroughCounts = boroughCounts[(boroughCounts["From"] != None) & (boroughCounts["To"] != None)]
但是它不起作用,所有值都保持不变。 我使用错了吗,还是有更好的方法呢?
答案 0 :(得分:1)
使用它,因为None是一个字符串,您需要用NaN替换该字符串:
df_out = boroughCounts.replace('None', np.nan).dropna()
df_out
输出:
From To Count
10 Bronx Bronx 51
11 Bronx Manhattan 244
13 Bronx Queens 100
或者您可以通过使用“无”来使用布尔索引:
boroughCounts[(boroughCounts.From != "None") & (boroughCounts.To != "None")]
答案 1 :(得分:1)
检查数据框以了解类型。
boroughCounts.dtypes
这将告诉您他的To和From cols是object类型。这可能意味着它们都是字符串或字符串和None类型的组合。检查您的其中一位。
type(boroughCounts.iloc[15].From)
这将向您显示第15行的“从”列中的“无”是否为字符串。如果是这样,则需要更改查询。