gpt4 book ai didi

python - Pandas 过滤或删除行多个条件

转载 作者:行者123 更新时间:2023-12-01 01:48:45 27 4
gpt4 key购买 nike

我有这个数据框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

尝试使用描述的方法过滤掉“From”和“To”列中的 Noneherehere :

boroughCounts = boroughCounts[(boroughCounts.From != None) & (boroughCounts.To != None)]

boroughCounts = boroughCounts[(boroughCounts["From"] != None) & (boroughCounts["To"] != None)]

但它不起作用,所有值都保持原样。是我用错了,还是有更好的方法?

最佳答案

使用这个,因为 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

或者您可以通过使用“None”来使用 bool 索引:

boroughCounts[(boroughCounts.From != "None") & (boroughCounts.To != "None")]

关于python - Pandas 过滤或删除行多个条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50949202/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com