0

I have one dataFrame like

col_1 col_2
1       0
1       1 
1       2
2       0
2       1
2       2

and I want to convert into

col_1    a     b     c
1        0     1     2    
2        0     1     2
Umar.H
  • 22,559
  • 7
  • 39
  • 74
  • 1
    try this: `import string` , next you can map the `string.ascii_lowercase` for naming columns and then unstack , `(df.set_index(['col_1',df.groupby('col_1').cumcount().map(dict(enumerate(list(string.ascii_lowercase))))])['col_2'].unstack().reset_index())` ... Does this help? – anky Feb 07 '20 at 11:28
  • 1
    @anky_91 oh same idea :) was looking through my github for the my code to make map from numbers to alphabet – Umar.H Feb 07 '20 at 11:32
  • @Datanovice aha.. I see :) however if OP wants exactly same logic, then probably this isn't a dupe also realized that you dont need the list there: `...map(dict(enumerate(string.ascii_lowercase)))` should work fine :) – anky Feb 07 '20 at 11:34
  • you are right @anky_91 I put that in my code base last year when I just started to learn how to code (: i think OP can get his answer from comments. Naveen just @ me here if you need any help – Umar.H Feb 07 '20 at 11:39
  • @Datanovice thanx it's working :) – Naveen Rishishwar Feb 07 '20 at 11:43
  • 1
    just incase you end up with more than 26 rows per group , you can consider using the similar solution with a custom mapper function as mentioned [here](https://stackoverflow.com/questions/23861680/convert-spreadsheet-number-to-column-letter) : `df.groupby('col_1').cumcount().add(1).map(colnum_string)` or `import xlsxwriter ` , `df.groupby('col_1').cumcount().map(xlsxwriter.utility.xl_col_to_name).str.lower()` – anky Feb 07 '20 at 11:49

0 Answers0