保存具有特定名称的数据框的列表。

huangapple go评论68阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年5月22日 11:52:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76302938.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定