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

时间:2019-03-11 13:40:41

标签: python pandas dataframe

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

我确实是这样的:

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'值不被视为字符串。

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

谢谢

1 个答案:

答案 0 :(得分:2)

如果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