1

I have the following data in a pandas df:

   Season | Answer 
   ------ | ------
0    4    | 0
1    4    | 0
2    2    | 0
3    1    | 1
4    4    | 1
5    2    | 1
6    3    | 0
7    1    | 0.5
8    4    | 1
9    4    | 0   

I want to count how many days of each season have Answer=1, how many with answer=0.5, and how many with answer=0 to get next result:

   Season | Answer | Quantity 
   ------ | ------ | --------
0    4    | 0      | 3
1    4    | 0      | 3
2    2    | 0      | 1
3    1    | 1      | 1
4    4    | 1      | 2
5    2    | 1      | 1
6    3    | 0      | 1
7    1    | 0.5    | 1
8    4    | 1      | 2
9    4    | 0      | 3
vestland
  • 55,229
  • 37
  • 187
  • 305
BohdanS
  • 111
  • 1
  • 8
  • https://stackoverflow.com/questions/19384532/how-to-count-number-of-rows-in-a-group-in-pandas-group-by-object – Dadep Jul 13 '17 at 14:14

1 Answers1

2

Let's try groupby with transfrom:

df['Quantity'] = df.groupby(['Season','Answer'])['Answer'].transform('size')

Output:

   Season  Answer  Quantity
0       4     0.0       3.0
1       4     0.0       3.0
2       2     0.0       1.0
3       1     1.0       1.0
4       4     1.0       2.0
5       2     1.0       1.0
6       3     0.0       1.0
7       1     0.5       1.0
8       4     1.0       2.0
9       4     0.0       3.0
Scott Boston
  • 147,308
  • 15
  • 139
  • 187
  • Thanks, It works. Can you explain how you hit upon this idea? – BohdanS Jul 13 '17 at 14:28
  • 1
    First, you need to understand the concepts listed in the Pandas documentation under [Group By: split-apply-combine](http://pandas.pydata.org/pandas-docs/stable/groupby.html#group-by-split-apply-combine). I used [`transform`](http://pandas.pydata.org/pandas-docs/stable/groupby.html#transformation) because wanted my output to match the same length as my source dataframe and we are calculating on a single column. And, `size` counts all values in that group including Nans just incase you any missing values. – Scott Boston Jul 13 '17 at 14:32