I have a pandas dataframe (df
) of the following format:
+------+-------+-------+
| Zone | Group | Count |
+------+-------+-------+
| 897 | 1 | 78 |
| 897 | 2 | 49 |
| 897 | 3 | 23 |
| 482 | 1 | 157 |
| 482 | 2 | 57 |
| 482 | 3 | 28 |
+------+-------+-------+
I would like to alter the dateframe so that there exists only one row per Zone
. The output would be...
+------+----------+----------+----------+
| Zone | Count_G1 | Count_G2 | Count_G3 |
+------+----------+----------+----------+
| 897 | 78 | 49 | 23 |
| 482 | 157 | 57 | 28 |
+------+----------+----------+----------+
In terms of generating the new column names, I think the best method would be to use some automated counter-based method. I have provided sample data, but the actual problem I am working on has hundreds of rows of data to be transformed in this manner.
The following post addresses one approach to naming new columns based on dictionaries, which would be a less than ideal approach in this case.