Home > OS >  How to sum the grouping results of a column in python?
How to sum the grouping results of a column in python?

Time:04-24

I have this code and I need to sum the variable category which the count value, I'm trying this. Without any additional library

import json
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import base64
dfex2 = pd.DataFrame({'var': ["A", "A", "B", "A","C"], 'count': [1,2,5,1,0]})
dfex2.groupby(['var','count'])['count'].count()

And i'm getting this result.

var  count
A    1        2
     2        1
B    5        1
C    0        1
Name: count, dtype: int64

but I need something like this:

var  result
A    4
B    5
C    0        

CodePudding user response:

Shouldn't you sum instead ?

dfex2.groupby(['var'])['count'].sum().reset_index()

True, as the comment said, there is no much explanation:

  • We group by 'var', we consider the 'count' column for which we sum the values, then reset the index so we get a df instead of a series
  • Related