
正文
pandas中一列含有多种数据类型的转换:科学计算法转浮点数、字符映射
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
import pandas as pd
import redef getNum(x):
"""
科学计数法和字符转浮点数
"""
if re.findall(r'\d+\.\d+E\+',x):
return "%.f" % float(x)
elif x=="C":
return 1
else:
return xdf = pd.DataFrame({"x":[2030,1.11002E+11,2030,1.11002E+11,"C"]})df["x"] = df["x"].astype("str")df["x"] = df["x"].apply(getNum)df["x"] = pd.to_numeric(df["x"])df["x"] = df["x"].astype("int64")







