gpt4 book ai didi

python - 根据列值等于 None 的条件删除 DataFrame 中的行

转载 作者:行者123 更新时间:2023-12-01 01:08:28 25 4
gpt4 key购买 nike

我有一个数据框,其中有一列“状态”我尝试删除“状态”列包含“无”值的所有行。

我确实喜欢这样:

oppty_oppline.dropna(subset = ['status'])

但是“None”值并未被删除。我这样验证:

oppty_oppline.status.unique()

结果:

array(['Cancelled by ', 'Cancelled by Customer',
'Account not selected', None,
'Won - Deliver & Validate by ', 'Lost',
'Won - Deliver & Validate by Partner',
'Won-Deliver&Validate by ',
'Cancelled by ', 'Won by another',
'Won- Deliver and Validate by Partner',
'Won – Deliver & Validate by Partner'], dtype=object)

我发现“None”值不被视为字符串。

有什么想法可以帮助我吗?

谢谢你

最佳答案

如果None认为它工作得很好:

a = np.array(['Cancelled by ', 'Cancelled by Customer',
'Account not selected', None])

oppty_oppline = pd.DataFrame({'status':a})
print (oppty_oppline)
status
0 Cancelled by
1 Cancelled by Customer
2 Account not selected
3 None

df = oppty_oppline.dropna(subset = ['status'])
print (df)
status
0 Cancelled by
1 Cancelled by Customer
2 Account not selected

但是如果字符串None需要通过 boolean indexing 删除行:

a = np.array(['Cancelled by ', 'Cancelled by Customer',
'Account not selected', 'None'])

oppty_oppline = pd.DataFrame({'status':a})
print (oppty_oppline)
status
0 Cancelled by
1 Cancelled by Customer
2 Account not selected
3 None

#not remove None, because string
df = oppty_oppline.dropna(subset = ['status'])
print (df)
0 Cancelled by
1 Cancelled by Customer
2 Account not selected
3 None
<小时/>
df = oppty_oppline[oppty_oppline.status != 'None']
print (df)
0 Cancelled by
1 Cancelled by Customer
2 Account not selected

关于python - 根据列值等于 None 的条件删除 DataFrame 中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55103150/

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