I have a dataframe like as below
ID,design_id,year,category
1,21345,1978,DC
1,3456,2019,DC
1,5678,2021,DF
1,7890,2021,DC
1,5678,2021,OT
1,1357,2020,np.nan
2,9876,2021,DC
2,9865,2021,DC
2,9876,2021,DC
I would like to do the below
a) create 3 year columns (2020,2021,2022) for variables design_id, category == DC and category == DF.
b) put the count of unique design_id for each ID under the respective year columns
c) put the count of category == DC and category == DF for each ID and design_id combo under the respective year columns
I tried the below but this only works for requirement a) above.
pd.crosstab(
index=tf['ID'], columns=tf['year'],
values=tf['design_id'], aggfunc='nunique').fillna(0)
My real dataframe has 4 million rows and has 50 unique year values.(from 1970 to 2022)
I expect my output to be like as below
ID,design_cnt2020,design_cnt2021,design_cnt2022,DC_cnt_2020,DC_cnt_2021,DC_cnt_2022,DF_cnt_2020,DF_cnt_2021,Df_cnt_2022
1,1,2,0,0,1,0,0,1,0
2,0,2,0,0,2,0,0,0,0