本文共 538 字,大约阅读时间需要 1 分钟。
问题:
有一个DataFrame,列名为:['$a', '$b', '$c', '$d', '$e']
现需要改为:['a', 'b', 'c', 'd', 'e']
有何办法? import pandas as pddf = pd.DataFrame({ '$a': [1], '$b': [1], '$c': [1], '$d': [1], '$e': [1]})
解决:
# ①暴力df.columns = ['a', 'b', 'c', 'd', 'e']# ②修改df.columns = df.columns.str.strip('$')# ③修改df.columns = df.columns.map(lambda x:x[1:])
# ④暴力(好处:也可只修改特定的列)df.rename(columns=('$a': 'a', '$b': 'b', '$c': 'c', '$d': 'd', '$e': 'e'}, inplace=True) # ⑤修改df.rename(columns=lambda x:x.replace('$',''), inplace=True)
转载地址:http://yrjil.baihongyu.com/