python数据清洗值-把一列的年、月、日替换成.

超级快,谁能告诉我为什么可以整列replace??

数据格式如下:

大神写的方式:

python操作方式:

def data_time(df,*cols):
    for col in cols:
        df[col] = df[col].str.replace('年','.')
        print(df[col])
        df[col] = df[col].str.replace('月','.')
        print(df[col])
        df[col] = df[col].str.replace('日','')
        df[col] = pd.to_datetime(df[col])
    return(df)
    

data_c2=data_time(data_c1,'数据获取日期')
print(data_c2.head())

结果如下:

我写的方式:

def data_time(df,col):
    rows=len(df[col])
    for row in range(rows):
        df[col][row] = df[col][row].replace('年','.')
        df[col][row] = df[col][row].replace('月','.')
        df[col][row] = df[col][row].replace('日','')
        df[col][row] = pd.to_datetime(df[col][row])
    return(df)

结果是一样的,但是我的速度超级慢,超级慢,怎么肥事。who can tell me!!!

发布了49 篇原创文章 · 获赞 9 · 访问量 3458

猜你喜欢

转载自blog.csdn.net/qq_39817865/article/details/102642471