简体   繁体   English

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

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

I have a dataframe in which there is a column "status" I try to delete all rows in which "status" columns contains None value. 我有一个数据框,其中有一列“状态”,我尝试删除其中“状态”列包含“无”值的所有行。

I did like this : 我确实是这样的:

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

But The "None" values wasn't deleted . 但是“ None”值没有被删除。 I verify like this : 我这样验证:

oppty_oppline.status.unique()

Result : 结果:

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)

I see that 'None' values is not considered as a string . 我看到'None'值不被视为字符串。

Any idea please to help me? 有什么想法请帮我吗?

Thanks you 谢谢

If None value it working nice: 如果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

But if string None need remove rows by boolean indexing : 但是如果字符串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