0

Using pandas and I have a DataFrame like this:

+-------+---+
|  item | x |
+-------+---+
| item1 | 0 |
+-------+---+
| item1 | 5 |
+-------+---+
| item1 | 6 |
+-------+---+
| item2 | 2 |
+-------+---+
| item2 | 3 |
+-------+---+
| item2 | 1 |
+-------+---+

And I'd like to merge the repeated values from the first column and expand the second column into a series just like this:

+-------+----+----+----+
|  item | x1 | x2 | x3 |
+-------+----+----+----+
| item1 |  0 |  5 |  6 |
+-------+----+----+----+
| item2 |  2 |  3 |  1 |
+-------+----+----+----+
maroov
  • 43
  • 6
  • Welcome to SO. This isn't a discussion forum or tutorial. Please take the [tour] and take the time to read [ask] and the other links found on that page. – wwii May 31 '20 at 15:00
  • 2
    `out = df.set_index(['item',df.groupby('item').cumcount().add(1)]).unstack()` then `out.columns = out.columns.map('{0[0]}{0[1]}'.format) ` see Q10 of linked Q&A – anky May 31 '20 at 15:03
  • @wwii The thing is, this looks pretty simple but I haven't found a tutorial page or answered question for it. Maybe it's a newbie question, but it is still a question. If there is a newbie pandas forum I can post on please let me know and I'll do it in there. – maroov May 31 '20 at 15:04
  • @anky Thank you, this does the job. – maroov May 31 '20 at 15:06

0 Answers0