I know how to unstack rows into columns, but how to deal with the following dataframe?
| date | dummy | avg | lable |
|---|---|---|---|
| 1-19 | 1 | 20 | l1 |
| 1-19 | 0 | 40 | l1 |
| 1-27 | 1 | 100 | l2 |
| 1-27 | 0 | 140 | l2 |
Expected dataframe:
| date | avg_t | avg_c | lable |
|---|---|---|---|
| 1-19 | 20 | 40 | l1 |
| 1-27 | 100 | 140 | l2 |
The avg is 20 when dummy equals 1 and it is renamed to avg_t as a column. Similar to the column avg_c.
I tried:
df.groupby(['dummy','avg']).size().unstack
But, It does not work.