0

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?

1 Answers1

0

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')
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • If then i want to read those pickle files as dataframes. How i should mofidy this code. So the code: i= pd.read_pickle (file_pkl+str(i)+'.pkl', compression='gzip') will work – Constantinos Galanakis Mar 19 '21 at 11:18