Before I start, I want to make it clear that my question is different than Counting values that meet a condition and Want to count the number of values in a column that meet a condition.
Allow me to explain. Here is my df:
test = pd.DataFrame({'name':['joe','dan','betty','joe','dan','betty','joe','dan','betty','joe','dan','betty'],'points':[12,3,5,10,5,16,2,8,15,17,1,3]})
test
name points
0 joe 12
1 dan 3
2 betty 5
3 joe 10
4 dan 5
5 betty 16
6 joe 2
7 dan 8
8 betty 15
9 joe 17
10 dan 1
11 betty 3
What I am aiming to do is count how many times each person had less than 10 points and create a new column that consists of that value. I tried the following and got really close:
test['<10'] = test[test['points'] < 10].groupby('name')['points'].transform('count')
test
name points <10
0 joe 12 NaN
1 dan 3 4.0
2 betty 5 2.0
3 joe 10 NaN
4 dan 5 4.0
5 betty 16 NaN
6 joe 2 1.0
7 dan 8 4.0
8 betty 15 NaN
9 joe 17 NaN
10 dan 1 4.0
11 betty 3 2.0
I get the values I want, but since I subset the data frame to values <10, I am left with NaN's in the rows that were excluded. I almost have this figured out, but I would like to get those NaN values filled to show how many times each person had less than 10 points (ie joe should have 1, betty 2, and dan 4). Any help is appreciated, thanks!