繁体   English   中英

按等于无值的列值条件在DataFrame中删除行

[英]Drop rows in DataFrame by conditions on column values equal to None

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

我确实是这样的:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM