I'm new to pandas and was looking for some advice on how to reshape my dataframe:
Currently, I have a dataframe like this.
| panelist_id | type | type_count | refer_sm_count | refer_se_count | refer_non_n_count | |
|---|---|---|---|---|---|---|
| 1 | HP | 2 | 2 | 1 | 1 | |
| 1 | PB | 1 | 0 | 1 | 0 | |
| 1 | TN | 3 | 0 | 3 | 0 | |
| 2 | HP | 1 | 1 | 0 | 0 | |
| 2 | PB | 2 | 1 | 1 | 0 | 0 |
Ideally, I want my dataframe to look like this:
| panelist_id | type_HP_count | type_PB_count | type_TN_count | refer_sm_count_HP | refer_se_count_HP | refer_non_n_count_HP | refer_sm_count_PB | refer_se_count_PB | refer_non_n_count_PB | refer_sm_count_TN | refer_se_count_TN | refer_non_n_count_TN |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 2 | 1 | 3 | 2 | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 0 |
| 2 | 1 | 2 | 0 | 1 | 0 | 0 | 1 | 1 | 0 | 0 | 0 | 0 |
Basically, I need to convert the different row values in the 'type' column into new columns, showing the count for each type. The next three columns on the original df titled 'refer' need to account for each different 'type'. e.g., refers_sm_count_[from type X (e.g., HP)]. Any help would be much appreciated. Thanks