1

I have a dictionary in which I've put several dataframes (all identical at this point). I'm trying to add data into the same column of each of those dataframes (fiscal year) corresponding to the key by which each dataframe can be called. The keys that I've assigned are the fiscal years. When I try to use dict.items(), however, it assigns each of the dataframes the same value (the last fiscal year). The goal is to forecast the revenue by fiscal year where I will break revenue into a new column according to how much will be garnered in each year. I've simplified my code to the below:

import pandas as pd
columns = ['ID','Revenue','Fiscal Year']
ID = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Revenue = [1000, 1200, 1300, 100 ,500, 0, 800, 950, 4321, 800]
FY = []
d = {'ID': ID, 'Revenue': Revenue}
df = pd.DataFrame(d)
df['Fiscal Year'] = ''
dataframe_dict = {}
def df_dict_func(start, end, dataframe):
    date_range = range(start, end + 1)
    for n in date_range:
        dataframe_dict[n] = dataframe
    for key, value in dataframe_dict.items():
        value['Fiscal Year'] = key
df_dict_func(2018, 2025, df)
print(dataframe_dict[2019])
DJHeels
  • 89
  • 8

1 Answers1

0

Seems unnecessary to create the dict keys and values in one loop and then add a column name in another loop. Instead your code should look something like this

import pandas as pd

def df_dict_func(start, end, dataframe):
    date_range = range(start, end + 1)
    dataframe_dict = {}
    for n in date_range:
        sub = dataframe.copy()
        sub['Fiscal Year'] = n
        dataframe_dict[n] = sub
    return dataframe_dict


columns = ['ID','Revenue','Fiscal Year']
ID = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Revenue = [1000, 1200, 1300, 100 ,500, 0, 800, 950, 4321, 800]
FY = []
d = {'ID': ID, 'Revenue': Revenue}
df = pd.DataFrame(d)

df_dict = df_dict_func(2018, 2025, df)

print(df_dict[2019])
DJK
  • 8,924
  • 4
  • 24
  • 40