1

I have a following pandas df:

df = { "id" : [1, 1, 1, 2, 2, 5], "name" : ["apple", "potato","lemon", "apple","potato","apple"]}
df = pd.DataFrame(df)

I would like to turn name column into rows based on id. Desired output is:

new_df = {1 : ["apple", "apple", "apple" ], 2: ["potato", "potato", ""], 3: ["lemmon", "", ""]}
new_df = pd.DataFrame(new_df)

How can I do it please?

Nick
  • 4,820
  • 18
  • 31
  • 47
vojtam
  • 1,157
  • 9
  • 34
  • Check the #10 in the duplicate – mozway Aug 22 '23 at 10:14
  • @mozway @vojtam do we realize the `new_df` expected output has nothing to do with a pivoted version of `df`? The `id` column in `df` has `1, 2, 5` values, while the `new_df` columns are `1, 2, 3` ?? – jlgarcia Aug 22 '23 at 10:17
  • @jlgarcia `df.assign(col=df.groupby('id').cumcount()).pivot(index='id', columns='col', values='name')` seems pretty close, OP can `factorize` the IDs if they are not meaningful. Or maybe `df.pivot('id', 'name', 'name')`. – mozway Aug 22 '23 at 10:22

0 Answers0