0

First question here, sorry if I make any mistakes.

I'm learning python and pandas dataframes. I have a very large dataframe that has data for many years and I'd like to separate it in diferent dataframes filtered by years.

Say for instance that for df_general there are 10 years. Is there some way that I can call a for loop that dinamically assign the years to the variable names? I already have a function sliceDataframeByYear ready.

Basically what I want do is something like this

    for i in range(10):
        filteredDataframe = sliceDataframeByYear(df_general,i)
        dataframeYear + i = filteredDataframe

But when I run this I'd like to have dataframeYear + i as a the new object name, so after running something similar to that I'd be able to call say dataframeYear0, dataframeYear1 and so on directly.

Currently I have to do something like this:

    dfYear2021 = sliceDataframeByYear(df_general,2021)
    dfYear2020 = sliceDataframeByYear(df_general,2021)
    dfYear2019 = sliceDataframeByYear(df_general,2019)

My idea is to have a for loop that automatically create those variables with the name and corresponding data.

Is there a way to do something like that?

William Pursell
  • 204,365
  • 48
  • 270
  • 300
Guilherme
  • 1
  • 1

0 Answers0