简体   繁体   中英

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 . 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 .

Any idea please to help me?

Thanks you

If None value it working nice:

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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