Adding same value in the same column

Viewed 60

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

1 Answers

You can use the following approach:

df.groupby("Number")["Alphabet"].apply(lambda x:" ".join(x)).to_frame()
Related