I have a series of indexes in a dataframe s, which ranges from different states in a series of 0-9. Here is a snapshot of the dataframe.
How can we sum the series in such a way that it contains sum of all series for a single state.
Eg- (MD,0,1,2,3,4,5,6,7,8,9,10)- The sum of its all individual values
(NJ, 0, 1,2,3,4,5,6,7,8,9,10)- The sum of its all individual values
CodePudding user response:
Assuming that your DataFrame is dt
and that your index is made by tuples:
dt.groupby([x[0] for x in dt.index])[["0"]].sum()