Search code examples
pythonpandasgroup-by

How to count observations of a specific value according to some conditions in pandas dataframe -python


I have this kind of dataframe :

Tag     Error
4       10
3       8
2       11
3       7
4       14
3       5
4       6
2       8

I would like to calculate only the number of observations for values in the 'Error' column strictly smaller than 10 according to each 'Tag'.

I tried this code but it doesn't work:

Df.groupby('Tag')(['Error']<10).value_counts()

Solution

  • You almost got it. I think something like the following should work:

    df[df['Error'] < 10].groupby('Tag').value_counts()