核心方法是:
s = df.columns.to_series()
df.columns = s.map(dic).fillna(s)
使用示例
import pandas as pd
if __name__ == '__main__':
df = pd.DataFrame([
[0, 1, 1],
[0, 1, 1],
[0, 2, 2],
[0, 2, 3]
], columns=['a', 'b', 'c'])
dic = {'a': '1', "b": "2"}
s = df.columns.to_series()
df.columns = s.map(dic).fillna(s)
print(df.columns.tolist())
结果:['1', '2', 'c']