Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Group only by rows that belong to list

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading