簡體   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