import pandas as pd
import numpy as np
df = pd.DataFrame([
[1, 0, np.nan, pd.NaT, None, ""]
]).T
def conv_num(x):
if pd.isna(x):
return np.nan, "缺失值"
try:
return float(x), "浮点数"
except:
return np.nan, "异常"
print(
pd.concat([
df,
df.applymap(conv_num),
], axis=1)
)
0 0
0 1 (1.0, 浮点数)
1 0 (0.0, 浮点数)
2 NaN (nan, 缺失值)
3 NaT (nan, 缺失值)
4 None (nan, 缺失值)
5 "" (nan, 异常)