2

Hey I would like to turn python table from

Number Alphabet
1 A
1 B
1 C
2 A
2 B
3 A

To table

Number Alphabet
1 A B C
2 A B
3 A

I know how to add normal table using dataframe

import pandas as pd

data = {'Alphabet': ['A', 'B', 'C', 'D', 'E'],
        'Number': [1, 2, 3, 4, 5]
        }

df = pd.DataFrame(data)

print (df)

But not the second table

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
AccelUp
  • 31
  • 4
  • Your question has already been answered here: https://stackoverflow.com/questions/22219004/how-to-group-dataframe-rows-into-list-in-pandas-groupby – mkrieger1 Dec 14 '21 at 08:22
  • thank you, the problem are solved with the same question – AccelUp Dec 14 '21 at 08:31

1 Answers1

0

You can use the following approach:

df.groupby("Number")["Alphabet"].apply(lambda x:" ".join(x)).to_frame()
nikeros
  • 3,302
  • 2
  • 10
  • 26