Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display sum function value for only one category

data = {'Person': ['a','b','c','d','a','b','c','d','b','c'],
        'months':['Jan','Jan','Jan','Jan','Feb','Feb','Feb','Feb','March','March'],
        'income':[100,75,80,56,48,56,37,48,95,65]}
df = pd.DataFrame(data)

df.groupby(['Person'])['income'].sum()

Output:

Person  
a    148  
b    226  
c    182  
d    104  
Name: income, dtype: int64

But I want to display data for only a. How can I do that?

like image 265
Lalit Jain Avatar asked May 01 '26 18:05

Lalit Jain


2 Answers

Why use groupby if you only need a?

df.loc[df["Person"].eq("a"),"income"].sum()

#148
like image 90
Henry Yik Avatar answered May 04 '26 09:05

Henry Yik


df[df['Person'] == 'a'].groupby(['Person'])['income'].sum()

Output

Person
a    148
Name: income, dtype: int64
like image 38
iamklaus Avatar answered May 04 '26 10:05

iamklaus