Pandas groupby - когда столбец имеет определенное значение - PullRequest
0 голосов
/ 02 августа 2020

Для следующей таблицы:

enter image description here

I would like to use the groupby function with 'number_of_horses' and show a count for the 'TRUE' values in the 'winner' column.

Using Python-Pandas, I have tried:

INPUT:

df.groupby('number_of_horses').winner.count()

ВЫХОД:

enter image description here

The table above is not the desired output.

The desired output is:

введите описание изображения здесь

Обратите внимание, что столбец «Победитель» засчитывается, только если значение TRUE.

1 Ответ

0 голосов
/ 02 августа 2020

Здесь вы go:

df[df.winner == True].groupby('number_of_horses').winner.count().reset_index()                                                                                                                                         

Вывод

   number_of_horses  winner
0                 2       2
1                 3       6

Если столбец winner является строкой, используйте df.winner == "TRUE" в качестве критерия фильтрации

...