pandas中一列含有多种数据类型的转换:科学计算法转浮点数、字符映射

import pandas as pd
import re

def getNum(x):
    """
    科学计数法和字符转浮点数
    """
    if re.findall(r'\d+\.\d+E\+',x):
        return "%.f" % float(x)
    elif x=="C":
        return 1
    else:
        return x
        
        
        
df = 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")

猜你喜欢

转载自www.cnblogs.com/wzdLY/p/9885877.html
今日推荐