Edit: Preserved my original answer below, but I put it up without testing it and it actually doesn't work for me.
import pandas as pd
import numpy as np
ser1 = pd.Series(['hi',None,np.nan])
ser2 = pd.Series([5,7,9])
df = pd.DataFrame([ser1,ser2]).T
This is janky, I know. Also, apparently the DataFrame constructor (but not the Series constructor) coerces None to np.nan. No idea why.
df.loc[1,0] = None
So now we have
0 1
0 'hi' 5
1 None 7
2 NaN 9
df.columns = ['col1','col2']
mask = np.equal(df['col1'], None)
df.loc[mask, 'col1'] = []
But this doesn't assign anything. The dataframe looks the same as before. I'm following the recommended usage from the docs and assigning base types (strings and numbers) works. So for me the problem is assigning objects to dataframe entries. No idea what's up.
(Original answer)
Two things:
- I'm not familiar with
np.equal
but pandas.isnull()
should also work if you want to capture all null values.
- You are doing what is called "chained assignment". I don't understand the problem fully but I know it doesn't work. In the docs.
Try this:
mask = pandas.isnull(df[col])
df.loc[mask, col] = list()
Or, if you only want to catch None
and not np.nan
:
mask = np.equal(df[col].values, None)
df.loc[mask, col] = list()
Note: While pandas.isnull
works with None
on dataframes, series, and arrays as expected, numpy.equal
only works as expected with dataframes and arrays. A pandas Series of all None
will not return True for any of them. This is due to None
only selectively behaving as np.nan
See BUG: None is not equal to None #20442