聚合国内IT技术精华文章,分享IT技术精华,帮助IT从业人士成长

NaN value in DataFrame

2022-02-11 13:45 浏览: 2444387 次 我要评论(0 条) 字号:

NaN value in NumPy or Pandas is some type dangerous for data processing. Like the example below:

import numpy as np
import pandas as pd

df = pd.DataFrame({"col1": [None, None, None]})
df["col1"] = df["col1"].astype("float")

print(df)

print(df["col1"] >= 3.14)
print(df["col1"] < 3.14)
   col1
0   NaN
1   NaN
2   NaN
0    False
1    False
2    False
Name: col1, dtype: bool
0    False
1    False
2    False
Name: col1, dtype: bool

If we directly convert “None” to the “float” type, it will become the “NaN” value. The “Nan” couldn’t be compared with real float number therefore it is neither “bigger or equal than” a float-point number nor “smaller than” it.

Since only float-point type could allow the “None” value in a column, we should be much careful when processing with float-point number.



网友评论已有0条评论, 我也要评论

发表评论

*

* (保密)

Ctrl+Enter 快捷回复