Group only by rows that belong to list

Question:

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.

Asked By: Luiz Scheuer

||

Answers:

You can use where for that:

df.where(df.A.isin(my_list)).groupby('A')['B'].sum()
Answered By: bitflip

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
Answered By: Naveed
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.