英文:
Python - saving a list of dataframes with specific names
问题
I have a list of excel files obtained with glob. I want to save a sheet from each one as a dataframe with a specified name.
I want to do something like below, which returns TypeError: unhashable type: 'list'
data={}
for f in files:
week = re.sub('\D','',f[68:79])
df_name = ['Week ' + week]
data[df_name] = pd.read_excel(f,'Sheet1')
英文:
I have a list of excel files obtained with glob. I want to save a sheet from each one as a dataframe with a specified name.
I want to do something like below, which returns TypeError: unhashable type: 'list'
data={}
for f in files:
week = re.sub('\D', '',f[68:79])
df_name = ['Week ' + week]
data[df_name] = pd.read_excel(f,'Sheet1')
答案1
得分: 1
你得到的错误是因为列表是可变的,因此无法哈希。
你可以尝试更改下面的行 -
df_name = 'Week ' + week
英文:
The error you are getting is because lists are mutable an hence are unhashable.
You can try changing below line -
df_name = 'Week ' + week
答案2
得分: 0
将您的list
更改为tuple
。
data = {}
for f in files:
week = re.sub('\D', '', f[68:79])
df_name = ('Week ' + week,) # tuple in place of list
data[df_name] = pd.read_excel(f, 'Sheet1')
英文:
Change your list
to a tuple
.
data={}
for f in files:
week = re.sub('\D', '',f[68:79])
df_name = ('Week ' + week) # tuple in place of list
data[df_name] = pd.read_excel(f,'Sheet1')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论