S Ringne S Ringne - 1 month ago 9
Python Question

Select stament with division operator mysql using pandas dataframe

i have a table in my pandas dataframe:

+-----------------+-----------+
| CategoryCount | frequency |
+-----------------+-----------+
| 0 | 123 |
| 12 | 234 |
| 15 | 456 |
| 0 | 678 |
| 25 | 890 |
| 0 | 145 |
+-----------------+-----------+


it has 300 rows

I want to find total no. of Categorycount,where the frequency is less than 75%
i am writing this query in normal mysql to do so:

select count(CategoryCount)
from category_analysis
where (frequency * 100 / (select sum(frequency) from category_analysis)) < 75


how do i implement the same using python in pandas.

Answer

use value_counts and normalize

df.CategoryCount.value_counts(normalize=True).lt(.75).sum()