1

NaN value in DataFrame

 3 years ago
source link: http://www.donghao.org/2022/02/11/nan-value-in-dataframe/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

NaN value in DataFrame

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)
Python
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
Python
xxxxxxxxxx
   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.

Like this:

Loading...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK