Home > Mobile >  Count with conditional in pandas
Count with conditional in pandas

Time:02-14

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?

Thankss

CodePudding user response:

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.

CodePudding user response:

Problem solved!

Here's what i did

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)

filt_funcional = df['Eixo'] == 'Funcional'
filt_comercial = df['Eixo'] == 'Comercial'
filt_liderança = df['Eixo'] == 'Liderança'
filt_negocio = df['Eixo'] == 'Negócio'
filt_obr_cert = df['Eixo'] == 'Obrigatórios e Certificações'

df.loc[filt_funcional]['Nome'].value_counts()

Much easier than i thought!

And giving the credits, i only did bc of this video: https://www.youtube.com/watch?v=txMdrV1Ut64

  • Related