为了账号安全,请及时绑定邮箱和手机立即绑定

给定数据框中的列重命名,因为该列包含循环中的数据

给定数据框中的列重命名,因为该列包含循环中的数据

慕容708150 2021-03-31 13:45:16
场景:我有一个数据框列表。我试图重命名列并更改其顺序,但是列名称不完全匹配,例如:列可能是“ iterationlist”或“ iteration”。我尝试在一个循环内循环以读取所有列,如果名称包含我需要的内容,请更改该列的名称,但出现错误:TypeError: unhashable type: 'list'代码:import pandas as pdimport osfrom Tkinter import Tkfrom tkFileDialog import askdirectoryfrom os import listdirfrom os.path import isfile, joinimport glob# Get contentmypath = "//DGMS/Desktop/uploaded"all_files = glob.glob(os.path.join(mypath, "*.xls*"))contentdataframes = [pd.read_excel(f).assign(Datanumber=os.path.basename(f).split('.')[0].split('_')[0], ApplyOn='')     for f in all_files]#get list of dates and put to dfsfor dfs in contentdataframes:    dfs.rename(index=str, columns={[col for col in dfs.columns if 'iteration' in col]: "iterationlistfinal"})问题:执行此操作的正确方法是什么?
查看完整描述

2 回答

?
繁花如伊

TA贡献2012条经验 获得超12个赞

旧的,但解决方案很简单

dfs.rename(index=str, columns={col : "iterationlistfinal" for col in dfs.columns if 'iteration' in col} )

您无法在字典中传递清单


查看完整回答
反对 回复 2021-04-27
  • 2 回答
  • 0 关注
  • 146 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号