Grouping values inside of a dict of Dataframes

Scenario: I have one dict of Dataframes. Each of those Dataframes contains the data for one year (2017 to 2022). They have each two columns, the Code and the Value (where the value column name is simply the year of that given Dataframe).

Input Data Sample:

Code    2017
33200   6957
33200   151906
33200   142025
33200   729494
33200   68842
32420   153499
32320   1756310
32320   33949
32310   81860
32310   56127
32200   165520

Each of the Dataframes has the same list of codes, only difference is the year.

Expected output:

Code    2017
33200   1099224
32420   153499
32320   1790259
32310   137987
32200   165520

Objective: I am trying to do a groupby code to sum each value for that given code (similar to an SUMIF).

Issue: When I run the code below, the output dictionary is exactly the same as the input.

Code:

year_list_1 = [2017,2018,2019,2020,2021,2022]
sales_dict_2={}
for year_var in year_list_1:
    sales_dict_2[year_var] = sales_dict[year_var].groupby('Code',as_index=False)[[year_var]].sum() # where sales_dict is the dictionary mentioned above
    

Question: Why is this code outputting the same DF as the input DF?



Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation