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

Pandas: count values based on another column name

How to caunt all values from col2 based on col1 name with Pandas?

Input:

col1     col2
-------------
one      1
two      2
three    1
one      4
two      4
three    2

Output:

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

col1     col2
-------------
one      5
two      6
three    3

>Solution :

Use GroupBy.sum

df.groupby('col1', as_index=False)['col2'].sum()
# df.set_index('col1').sum(level='col1').reset_index() #decrepated in future

    col1  col2
0    one     5
1  three     3
2    two     6
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