Additional filter for groupby

Asked

Viewed 251 times

0

The following code groups my DF by some columns f0219.groupby(['Matricula', 'Nome', 'Rubrica', 'Valor', 'CodigoRendimentoDesconto', 'Tiporubrica']).Rubrica.count() and counts how many times the Rubric column appears in each grouping. The result of this code can be checked in the image below:

inserir a descrição da imagem aqui

I want, if the Tiporubrica column is equal to "2", it adds what is in the Value column, and displays the result of that sum in a third column. Also, I realized that when running the above groupby command, the result is not displayed on a dataframe, and I would like it to be, as I intend to work on the result as a dataframe.

1 answer

1

The solution to this case doesn’t need a condition. You can simply add one more grouping method in this groupby, in this case, the 'sum' for the 'Value' column'.

Also, to return a dataframe, simply reset the index.

Thus:

agg = {'Rubrica': 'count', 'Valor': 'sum'}

f0219.groupby(['Matricula', 'Nome']).apply(agg).reset_index()

If you need the other columns, just put which columns and which clustering methods in the Dict 'Agg'.

I hope I’ve helped :)

Browser other questions tagged

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