[英]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.