简体   繁体   English

保存多个数据框,从列表中选择它们

[英]save multiple dataframes selecting them from a list

When I write the code:当我写代码时:

TxtFiles = pd.Series (['Agreements',
                        'Assets',
                        'AssetsIFRS9Amount',
                        ....
                        'Loans',
                        'LoansProducts'
                        ])

# DataFrame_Name.to_pickle ('path/DataFrame_Name.pkl', compression='gzip')
file_pkl = "C://Users/.../060_Python_DataSets/"

for i in TxtFiles:
    print (i)
    str.strip(i).to_pickle (file_pkl+str(i)+'.pkl', compression='gzip')

I receive the message:我收到消息:

  File "<ipython-input-65-73534dab7869>", line 3, in <module>
    str.strip(i).to_pickle (file_pkl+str(i)+'.pkl', compression='gzip')

AttributeError: 'str' object has no attribute 'to_pickle'

The names in the pd.Series are the names of the dataframes and I want to save them as pickle. pd.Series 中的名称是数据框的名称,我想将它们保存为泡菜。 Can someone help to resolve this issue?有人可以帮助解决这个问题吗?

You have been caught by the terrible dynamic variable names .你被可怕的动态变量名所吸引。 You should instead build a dictionary mapping the names to the objects:相反,您应该构建一个将名称映射到对象的字典:

TxtFiles = {'Agreements': Agreements,
            'Assets': Assets,
             ....
            'Loans': Loans,
            'LoansProducts': LoanProducts
            }

Then you will be able to do:然后你就可以做到:

for i, df in TxtFiles.items():
    print (i)
    df.to_pickle (file_pkl+str(i)+'.pkl', compression='gzip')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM