我使用以下命令成功更改了数据框中的单个列名称:
df.columns=['new_name' if x=='old_name' else x for x in df.columns]
但是我有很多要更新的列(但不是所有240列),如果我能帮助的话,我不想每次更改都必须写出来。
我试图在此线程中遵循@StefanK的建议:
我的代码:
df.columns=[[4,18,181,182,187,188,189,190,203,204]]=['Brand','Reason','Chat_helpful','Chat_expertise','Answered_questions','Recommend_chat','Alternate_help','Customer_comments','Agent_category','Agent_outcome']
但我收到一条错误消息:
File "<ipython-input-17-2808488b712d>", line 3
df.columns=[[4,18,181,182,187,188,189,190,203,204]]=['Brand','Reason','Chat_helpful','Chat_expertise','Answered_questions','Recommend_chat','Alternate_help','Customer_comments','Agent_category','Agent_outcome']
^
SyntaxError: can't assign to literal
因此,在搜索了错误并阅读了更多其他问题之后,在我看来,它似乎试图将数字读取为整数而不是索引?我不确定在这里。
那么,我该如何解决它,以便将数字视为索引?我要替换的列名称每个都至少有10个字长,因此我希望不必全部输入!我唯一的想法是以某种方式使用iloc,但是我要在这里进入新领域!
非常感谢您的帮助
在代码中的df.columns之后删除“ =”,并改用它:
df.columns.values[[4,18,181,182,187,188,189,190,203,204]]=['Brand','Reason','Chat_helpful','Chat_expertise','Answered_questions','Recommend_chat','Alternate_help','Customer_comments','Agent_category','Agent_outcome']
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句