Change: saved my original answer below, but I raised it without testing, and it actually does not 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 junki, I know. Also, apparently, the DataFrame constructor (but not the Series constructor) casts None to np.nan. I have 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 that doesnβt mean anything. The data frame looks the same as before. I follow the recommended use from the documentation and assign the basic types (strings and numbers). So for me the problem is assigning objects to data items. I have no idea what happened.
(Original answer)
Two things:
- I am 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 a "chain assignment." I do not fully understand the problem, but I know that it does not work. In the docs .
Try it:
mask = pandas.isnull(df[col]) df.loc[mask, col] = list()
Or, if you want to catch only None , not np.nan :
mask = np.equal(df[col].values, None) df.loc[mask, col] = list()
Note. While pandas.isnull works with None on data frames, rows, and arrays, as expected, numpy.equal only works as expected with data frames and arrays. A series of pandas None will not return True for any of them. This is because None only behaves selectively as np.nan . See ERROR: No, Not Equal No, # 20442
exp1orer
source share