I want to create a column of counts in a pandas dataframe. Here is the input:
dict = {'id': [1,2,3,4,5,6], 'cat': ['A', 'A', 'A', 'A', 'A', 'B'], 'status': [1, 1, 1, 1, 2, 1]}
id cat status
0 1 A 1
1 2 A 1
2 3 A 1
3 4 A 1
4 5 A 2
5 6 B 1
Preferred output:
id cat status status_1_for_cat_count status_2_for_category_count
0 1 A 1 4 1
1 2 A 1 4 1
2 3 A 1 4 1
3 4 A 1 4 1
4 5 A 2 4 1
5 6 B 1 1 0
As can hopefully be seen, I'm trying to get the full counts added for each row to two columns (one for each status). I have tried several approaches, mostly with groupby in combination with unique_counts, transform, apply, filter, merges and what not, but have not been able to get this to work. I am able to do this on a single column easily (I want to create a column of value_counts in my pandas dataframe), but not with two different statuses combined with the category.