0

I am trying to write a for loop to create multiple dataframes based on dates. this is the basic code:

gl2 is a list of dataframe names

dts and dte are pandas timestamps

gl2=['aaj', 'cbd', 'cbu','fms','nyp']

for i in gl2:
   mask=(i['datetime']>=dts) & (i['datetime']<=dte)
   dfname=str(i+'e')
   dfname=i.loc[mask]

this returns a TypeError : string indices must be integers

mask=(aaj['datetime'] >= dts) & (aaj['datetime']<=dte)
aaje=aaj.loc[mask]

returns the desired dataframe, but i may need to create many such dataframes and want to automate the process

matt cooper
  • 101
  • 1
  • 8

1 Answers1

1

You could pull the variable from the locals() dictionary

Edited to assign dynamically as well, as suggested by @Ze'ev Ben-Tsvi

gl2=['aaj', 'cbd', 'cbu','fms','nyp']

for i in gl2:
   mask=(locals()[i]['datetime']>=dts) & (locals()[i]['datetime']<=dte)
   dfname=str(i+'e')
   locals()[dfname]=locals()[i].loc[mask]
oskros
  • 3,101
  • 2
  • 9
  • 28
  • This does not work. produces a dataframe called 'dfname', not 5 dataframes named 'aaje', 'cbde' ..... etc – matt cooper Aug 02 '22 at 11:40
  • 2
    replace dfname=locals()[i].loc[mask] with locals()[dfname]=locals()[i].loc[mask] – Ze'ev Ben-Tsvi Aug 02 '22 at 11:56
  • 1
    Or rather use a dictionary. Defining variables dynamically is an anti-pattern often resulting in unmaintainable and buggy code. – mozway Aug 02 '22 at 12:04
  • @mozway Good point. I thought of a solution in his specific context, but you are certainly right that keeping the dataframes in a dictionary would be a cleaner solution – oskros Aug 03 '22 at 08:56