import pandas as pd
import numpy as np
df = pd.DataFrame(
[
[np.nan, 'None', 3],
[np.nan, 5, 6],
[7, 8, 9]
], columns=['a', 'b', 'c']
)
df.replace({np.nan: None}, inplace=True)
print(df)
df.replace({'None': None}, inplace=True)
print(df)
a b c
0 None None 3
1 None 5 6
2 7 8 9
a b c
0 NaN NaN 3
1 NaN 5.0 6
2 7.0 8.0 9
this is small example fo my case. i wanna replace nan, "None" to None. so i use replace twice first replace method work fine as i thought, but nan was reborn in second replace and all int is changed to float because of nan. i have no idea about why nan is reborn df.replace({'None': None}, inplace=True), how can i fix it?
df.replace({'None': np.nan}, inplace=True)?df.replace({'None': None}).astype(pd.Int64Dtype()), docs