【发布时间】:2023-11-15 18:14:01
【问题描述】:
CSV 文件中的数据样本
Model,Displ,Cyl,Trans,Drive,Fuel,Cert Region,Stnd,Stnd Description,Underhood ID,Veh Class,Air Pollution Score,City MPG,Hwy MPG,Cmb MPG,Greenhouse Gas Score,SmartWay,Comb CO2
ACURA RDX,3.5,6,SemiAuto-6,2WD,Gasoline,FA,T3B125,Federal Tier 3 Bin 125,JHNXT03.5GV3,small SUV,3,20,28,23,5,No,386
import pandas as pd
df_18 = pd.read_csv('file name')
请求: 重命名所有列标签以用下划线替换空格并将所有内容转换为小写。 下面的代码确实有效,我不知道为什么
df_18.rename(str.lower().str.strip().str.replace(" ","_"),axis=1,inplace=True)
【问题讨论】:
-
df.columns = df.columns.str.lower().str.strip().str.replace(' ', '_')是链接.str函数的正确方法。