I have the following df:
df = pd.DataFrame({'A': ['foo', 'bar', 'exa', 'tru', 'foo', 'bar', 'exa', 'tru'],
'B': [10, 20, 30, 40, 50, 60, 70, 80]})
Output:
A B
0 foo 10
1 bar 20
2 exa 30
3 tru 40
4 foo 50
5 bar 60
6 exa 70
7 tru 80
And my_list
:
my_list = ['foo', 'bar']
I want to perform a df.groupy('A')['B'].sum()
but only for items in df['A']
that are in my_list
.
CodePudding user response:
You can use where for that:
df.where(df.A.isin(my_list)).groupby('A')['B'].sum()
CodePudding user response:
using loc
(df.loc[df['A'].isin(my_list)] # rows where A matches my_list
.groupby('A',as_index=False)['B'].sum()) #groupby and sum
A B
0 bar 80
1 foo 60