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 column a if column b condition

I have a dataframe with two columns as shown below.

location   date
paris       6    
paris       4    
rome        3    
paris       5    
paris       6    
rome        6    
paris       4

Now I would like to have counted the different areas in column a, if column b is equal to 6.
So the desired result would be:

Paris 2

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

Rome 1

I am a total noob tbh, the basic idea would be: If column "date" is 6, count the values in column location.

Thx for any help.

>Solution :

It would be something like:

df[df['date']==6].groupby('location').count()

The df[df['date']==6] part, filters your DataFrame to only include rows with date values equal to 6. groupby('location') group filtered DataFrame by location value and count() counts the number of rows in groups.

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