Confusing about numpy/pandas nan

Viewed 64
import pandas as pd
import io
x = """a,b
,bb
2,
,,"""

df = pd.read_csv(io.StringIO(x))
print(df)
     a    b
0  NaN   bb
1  2.0  NaN
2  NaN  NaN

print([[type(__) for __ in df[_]] for _ in df ])
[[float, float, float], [str, float, float]]

print([set(df[_]) for _ in df])
[{nan, nan, 2.0}, {nan, 'bb'}]

As you can see, all nans are float, but set(df.b) gives unique values. while it appears that the two nan in column a are different. Why?

Besides, what's the reason for the following differences

[type(_) for _ in df.a]
Out[370]: [float, float, float]

[type(df.a[i]) for i in range(len(df.a))]
Out[371]: [numpy.float64, numpy.float64, numpy.float64]

which are different (though all of float type, but different classes)

[type(_) for _ in df.b]
Out[374]: [str, float, float]

[type(df.b[i]) for i in range(len(df.b))]
Out[373]: [str, float, float]

which are the same

0 Answers
Related