pandas使用字典格式修改columns列名

pandas使用字典格式修改columns列名,第1张

pandas使用字典格式修改columns列名

核心方法是:

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']

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5657519.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存