简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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