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

Count with conditional in pandas

i’m having a problem trying to count diferents variables for the same Name. The thing is: i have a sheet with the Name of all my workers and i need to count how many trainings they had, but thoses trainings have different classifications: "Comercial", "Funcional" and others…

One of my columns is "Name" and the other is "Trainings". How can i filter those trainings and aggregate per name

   import pandas as pd
   import numpy as np

   xls = pd.ExcelFile('BASE_Indicadores_treinamento_2021 - V3.xlsx')

   df = pd.read_excel(xls, 'Base')
   display(df)

   df2 = df.groupby("Nome").agg({'Eixo':'count'}).reset_index()
   display(df2)

What im getting is the TOTAL of trainings per Name, but i need the count of all categories i have in trainings (there are 5 of them). Does anyone know what i need to do?

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

Thankss

>Solution :

df.groupby("Nome").agg(‘count’) should give you the total number of training for each person.
df.groupby(["Nome","Eixo"]).agg({‘Eixo’:’count’}) should give you the count per each person per each training.

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