Find most repeated value

Asked

Viewed 713 times

3

I’m trying to analyze some shoe sales data, but I’m having a hard time creating a function to find out which number the customer bought the most in the previous year.

I have a table with this data:

Cód. Cliente    CPF     Nome                            Sexo        Tamanho
5879099     37513584800 LOJA                            MASCULINO   35
5879099     37513584800 LOJA                            MASCULINO   23
5879099     37513584800 LOJA                            MASCULINO   17
5879099     37513584800 LOJA                            MASCULINO   37
5879099     37513584800 LOJA                            MASCULINO   17
3353800     2613618809  DULIO JOSE DE SOUSA DAMICO      MASCULINO   35
3353800     2613618809  DULIO JOSE DE SOUSA DAMICO      MASCULINO   39
3112300     29953652805 ROSANA DA SILVA FAGUNDES        FEMININO    34
6116202     39285701884 ANA CAROLINA DE FARIAS FRANCISCO    FEMININO    31

The table is much more than this, just a few example lines.

Well, what I need to know is which size is the most repeated by customer number.

Which number did he buy the most?

I couldn’t find a way to do that if someone had a light.

Thank you,

  • It would be something like df['Tamanho'].value_counts().idxmax()?

  • @Andersoncarloswoss believe that something like this, but for each CPF, I would have to create a single CPF column and a function to pick up the idmax by Cpf?

1 answer

2


Iuri you could use PIVOT TABLE (dynamic table) in pandas

That would be about it:

import pandas as pd
import numpy as np

df = pd.read_excel("SEU ARQUIVO")
table = pd.pivot_table(df,index=["CPF","Tamanho"],
               values=["Tamanho"],
               aggfunc=[np.count_nonzero],fill_value=0)

I used the 'read_excel' just as an example, in your case just fill the dataframe with your data.

The parameter 'index' assembles the columns of the dynamic table, that is, the columns of category you want to use

and in the 'aggfunc'( Aggregation function ) I’m using the count

In this Link has an interesting content about Pivot Table that can help more.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.