sum a column based on two different columns

I am trying to summarize subsec column by controlling year and ticker in the below table. Such that, a new column to be created and sum of df.subsec to be added here.

the table I haveenter image description here

the table I need in the endenter image description here

I tried this data science link by adjusting the code but it didnot work:

dfc['k']=dfc.groupby(['ticker', 'year'])['subsec'].sum().sum(level=['ticker', 'year'])

Answer

Modify a little the sum and use reset_index to adjust the dataframe.

df = (
    dfc.groupby(['ticker', 'year'])['subsec']
    .sum()
    .reset_index(name='sumsss')
)

Leave a Reply

Your email address will not be published. Required fields are marked *