I am trying to populate a dataframe with the following code:
df = pd.DataFrame(data=np.random.choice([1, np.nan], size=5))
0 1
1 1
2 NaN
3 1
4 1
Then:
df[df[0].isnull()]
2 NaN
So far, so good. But if I am modifying the 1 to '1' things get strange (imo).
df = pd.DataFrame(data=np.random.choice(['1', np.nan], size=5))
0 1
1 1
2 1
3 1
4 nan
Problems come with the isnull
df[df[0].isnull()]
Empty DataFrame
Columns: [0]
Index: []
How can I get the nan (which is a string) to behave like a NaN? I want to be able to filter quickly on all null/non-null values within my dataframe.
Thanks.
NaNis being converted to thestrnanwhich is surprising AFAIK realNaNrequirefloatdtype, in this case you'd have to compare with the strnanwhich is weird IMOstrnanback to the "normal"NaN.NaN