1

I have a single pandas dataframe time series that is incremented in 5 minutes. I want to assign a variable name for each 5min increment. For example:

df_5min = df.resample('5min').first()
df_10min = df.resample('10min').first()
.
.
.
df_7200min = df.resample('7200min').first()

I would much rather keep these as separate dataframe names and keep them in ram rather than storing the dataframe and calling it later - by simple writing

for i in range(0,7201,5): df.to_csv('/path/df_' + str(i) + 'min.csv')

How can I assign a variable name for each so I can perform analysis on each dataframe independently within the same script?

Matt Elgazar
  • 707
  • 1
  • 8
  • 21

1 Answers1

1

You can create dictionary of DataFrames, because globals solution is not recommended:

#python 3.6+
dfs = {f'{x}min': df.resample(f'{x}min').first() for x in range(5,7201,5)}
#python bellow
dfs = {'{}min'.format(x): df.resample('{}min'.format(x)).first() for x in range(5,7201,5)}

Sample:

rng = pd.date_range('2017-04-03 12:15:10', periods=5, freq='11Min')
df = pd.DataFrame({'a': range(5)}, index=rng)  
print (df)
                     a
2017-04-03 12:15:10  0
2017-04-03 12:26:10  1
2017-04-03 12:37:10  2
2017-04-03 12:48:10  3
2017-04-03 12:59:10  4

dfs = {f'{x}min': df.resample(f'{x}min').first() for x in range(5,16,5)}
print (dfs)
{'5min':                        a
2017-04-03 12:15:00  0.0
2017-04-03 12:20:00  NaN
2017-04-03 12:25:00  1.0
2017-04-03 12:30:00  NaN
2017-04-03 12:35:00  2.0
2017-04-03 12:40:00  NaN
2017-04-03 12:45:00  3.0
2017-04-03 12:50:00  NaN
2017-04-03 12:55:00  4.0, '10min':                      a
2017-04-03 12:10:00  0
2017-04-03 12:20:00  1
2017-04-03 12:30:00  2
2017-04-03 12:40:00  3
2017-04-03 12:50:00  4, '15min':                      a
2017-04-03 12:15:00  0
2017-04-03 12:30:00  2
2017-04-03 12:45:00  3}

And then seelct by keys of dict:

print (dfs['5min'])
                       a
2017-04-03 12:15:00  0.0
2017-04-03 12:20:00  NaN
2017-04-03 12:25:00  1.0
2017-04-03 12:30:00  NaN
2017-04-03 12:35:00  2.0
2017-04-03 12:40:00  NaN
2017-04-03 12:45:00  3.0
2017-04-03 12:50:00  NaN
2017-04-03 12:55:00  4.0
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252