How to expand a set of columns using the first column's values as headers for the other columns?
For example:
x = pd.DataFrame({'id':[11,998,3923], 'count':[7,7,7],
'attributes':['VIS,TEMP,MIN','MIN,VIS,TEMP','MIN,VIS'],
'attribute_values':['0,4,2','2,3,0','0,9'],
'attribute_years':['2000,2001,2002','2001,2002,2003','2008,2009']})
(Edit: note that attributes could be out of order or missing.)
| index | id | count | attributes | attribute_values | attribute_years |
|---|---|---|---|---|---|
| 0 | 11 | 7 | VIS,TEMP,MIN | 0,4,2 | 2000,2001,2002 |
| 1 | 998 | 7 | MIN,VIS,TEMP | 2,3,0 | 2001,2002,2003 |
| 2 | 3923 | 7 | MIN,VIS | 0,9 | 2008,2009 |
In this case, the attributes column values should used to make new columns with attribute_values and attribute_years columns.
Ideal output:
| index | id | count | attribute_values_VIS | attribute_values_TEMP | attribute_values_MIN | attribute_years_VIS | attribute_years_TEMP | attribute_years_MIN |
|---|---|---|---|---|---|---|---|---|
| 0 | 11 | 7 | 0 | 4 | 2 | 2000 | 2001 | 2002 |
| 1 | 998 | 7 | 3 | 0 | 2 | 2002 | 2003 | 2001 |
| 2 | 3923 | 7 | 9 | NaN | 0 | 2009 | NaN | 2008 |