导包
import numpy as np
import pandas as pdfrom pandas import DataFrame
测试数据
columns = ['yy','fdl','jack_tom','mary lucy']
index = ['chinese','math','English']
data = np.random.randint(0,100,size=(3,4))
test = DataFrame(columns=columns, index=index, data=data)
test
test.columns
Index([‘yy’, ‘fdl’, ‘jack_tom’, ‘mary lucy’], dtype=‘object’)
将列名中空格( )换成下划线(_)
test.columns = test.columns.str.replace(' ', '_')
test.columns
Index([‘yy’, ‘fdl’, ‘jack_tom’, ‘mary_lucy’], dtype=‘object’)
将列名通过hash,映射更换
test.rename(columns={
'yy':'lyz','jack_tom':'jack tom'},inplace=True)
test.columns
Index([‘lyz’, ‘fdl’, ‘jack tom’, ‘mary_lucy’], dtype=‘object’)