1. 删除列
import pandas as pd
df.drop("Unnamed: 0", axis=1, inplace=True)
2. 转换列的格式
df["Date"] = df["Date"].astype("datetime64[ns]")
df = df.astype({'trans_time': 'datetime64[ns]', 'store_code'a:int, 'goods_id':int})
3. 数据膨胀
res_eff_gt3['weekday'] = [list(range(7))] * len(res_eff_gt3)
res_eff_gt3.explode('weekday')
4. 字符拆分与切分
df["Name"].str.split(",", expand=True)
# $19,100字符类型转成数值型
df["Payment"] = df["Payment"].str[1:].str.replace(",", ".").astype("float")
5. 正则:字符串替换
想要字母
df["Note"].str.replace('[^a-zA-Z]', '')
# 想要字母和数字
df["Note"].str.replace('[^a-zA-Z0-9]', '')
标签:技巧,df,res,astype,replace,str,pandas
From: https://www.cnblogs.com/tian1022/p/17203599.html