I'm extracting from a large dataframe the lists with the labels and the values, i'm storing them into a two separate lists, for example:
[['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'], [114.98, 17.4, 21.1, 7.99, 51.5, 3000.0, 7.99, 68.5, 19.99]]
after i want to store inside every month this list of labels and values, and after i want to store all the 12 months (into the code i use the numbers from 1 to 12) into a dictionay of the years
I would like to create a dictionary formatted in this way:
{
2018.0: {1: [['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'], [114.98, 17.4, 21.1, 7.99, 51.5, 3000.0, 7.99, 68.5, 19.99]], 2: [['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'], [114.98, 17.4, 21.1, 7.99, 51.5, 3000.0, 7.99, 68.5, 19.99]], 3: [['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'], [114.98, 17.4, 21.1, 7.99, 51.5, 3000.0, 7.99, 68.5, 19.99]], 4: [['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'], [114.98, 17.4, 21.1, 7.99, 51.5, 3000.0, 7.99, 68.5, 19.99]],
2019.0: {1: [['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'], [114.98, 17.4, 21.1, 7.99, 51.5, 3000.0, 7.99, 68.5, 19.99]], 2: [['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'], [114.98, 17.4, 21.1, 7.99, 51.5, 3000.0, 7.99, 68.5, 19.99]], 3: [['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'], [114.98, 17.4, 21.1, 7.99, 51.5, 3000.0, 7.99, 68.5, 19.99]], 4: [['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'], [114.98, 17.4, 21.1, 7.99, 51.5, 3000.0, 7.99, 68.5, 19.99]]
...
}
The problem with my code is that it store the same labels and values of all the months in all the years. As you can see from a piece of the code, i've censored only the labels, but in the code the labels and the values for all the months are equal, and is not correct. I know that is a little bit confusing. If something is not well explained let me know.
I've tried this:
the a
variable is from another For cycle that iterate the years, and the m
is for the
dma=dict.fromkeys(list_years) #how it looks dma {2018.0: None, 2019.0: None, 2020.0: None, 2021.0: None, 2022.0: None}
med=dict.fromkeys(list_months) #how it looks med {1: None, 2: None, 3: None, 4: None, 5: None, 6: None, 7: None, 8: None, 9: None, 10: None, 11: None, 12: None}
for argomento in lista_cat_pulita:
new_df = df.loc[(df["tipo"]=="Expense") & (df["mese"]==m) & (df["anno"]==a) & (df["categoria"]==argomento), "somma"].sum()
label.append(argomento)
value.append(new_df)
insieme.append(label)
insieme.append(value)
for ke, ve in dma.items():
if ke == a:
dma[ke]= med
for ki, vi in med.items():
if ki == m:
med[ki] = insieme
Edit
years = ['2001.0', '2002.0', '2003.0']
months={1:None, 2:None, 3:None, 4:None, 5:None, 6:None, 7:None, 8:None, 9:None, 10:None, 11:None, 12:None}
dyears=dict.fromkeys(years)
for k, v in dyears.items():
dyears[k]=months
for anno in years:
#other code
for mese in range(1,13):
#other code
for argomento in lista_cat_pulita:
new_df = df.loc[(df["tipo"]=="Expense") & (df["mese"]==m) & (df["anno"]==a) & (df["categoria"]==argomento), "somma"].sum()
mesecategorielabel.append(argomento)
meseValue.append(new_df)
insieme.append(mesecategorielabel)
insieme.append(meseValue)
for ki, vi in dyears.items():
if ki == anno:
for sub_k, sub_v in vi.items():
if sub_k == mese:
dyears[ki][sub_k]=insieme
print(dyears)
The problem is that for every months i've a different list, but i do not know why all the months of the years are overwritten with the last 12 lists of the last year.
CodePudding user response:
You want to create a dictionary of dictionaries. Use:
ml = [['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'], [114.98, 17.4, 21.1, 7.99, 51.5, 3000.0, 7.99, 68.5, 19.99]]
years = ['2001.0', '2002.0']
main_dict = {}
for year in years:
main_dict[year] = {i: ml for i in range(1,5)}
print(main_dict)