0

So I have an external system that I get some data from. It comes in this format:

+----+-----------+------------+-------+
| id | sessionId |  element   | value |
+----+-----------+------------+-------+
|  1 |         0 | extrainfo1 |    11 |
|  1 |         0 | extrainfo2 |    12 |
|  2 |         0 | extrainfo1 |    21 |
|  2 |         0 | extrainfo2 |    22 |
|  2 |         1 | extrainfo1 |    31 |
|  2 |         1 | extrainfo2 |    32 |
+----+-----------+------------+-------+

And I need it in this format:

+----+-----------+------------+------------+
| id | sessionId | extrainfo1 | extrainfo2 |
+----+-----------+------------+------------+
|  1 |         0 |         11 |         12 |
|  2 |         0 |         21 |         22 |
|  2 |         1 |         31 |         32 |
+----+-----------+------------+------------+

I have tried

sf_pivot = sessionfields.pivot(index='id', columns=['sessionId','element'], values='value')

But that returns this:

+-----------+------------+------------+------------+------------+
| sessionId |     0      |            |     1      |            |
+-----------+------------+------------+------------+------------+
| element   | extrainfo1 | extrainfo2 | extrainfo1 | extrainfo2 |
| id        |            |            |            |            |
| 1         | 11         | 12         |            |            |
| 2         | 21         | 22         | 31         | 32         |
+-----------+------------+------------+------------+------------+

I tried to unstack that but didn't seem to work.

To have sessionId as a value in index or value when doing the pivot doesn't seem to work either, I get duplicate error on that.

Gabbeh
  • 331
  • 1
  • 8

0 Answers0