INPUT TABLE
| pcd | INCOME | Education | age1to_20 | TG |
|---|---|---|---|---|
| a1001 | INCOME_1 | Education_1 | 1 | 1 |
| a1003 | INCOME_2 | Education_2 | 0 | 2 |
| a1001 | INCOME_3 | Education_2 | 5 | 2 |
| a1002 | INCOME_2 | Education_2 | 1 | 5 |
| a1003 | INCOME_1 | Education_2 | 3 | 4 |
REQUIRED OUTPUT
| pcd | INCOME_1 | INCOME_2 | INCOME_3 | Education_1 | Education_2 | age1to_20 | TG |
|---|---|---|---|---|---|---|---|
| a1001 | 1 | 0 | 1 | 1 | 1 | 6 | 1.5 |
| a1002 | 0 | 1 | 0 | 0 | 1 | 1 | 5 |
| a1003 | 1 | 1 | 0 | 0 | 2 | 3 | 3 |
pcd is index and income1,income2,income3,education1,education2,age are aggregated to sum while TG is aggregated to average.
pd.pivot_table(df, index=['pcd', 'age1to_20'],
aggfunc={'INCOME':sum,'Education'=sum,'age1to_20'=sum,'TG':avg},fill_value=0)
Tried above code but finding no success