0

How to change column wise data into row wise in excel using python (consider records in Millions)

Input

StuId  Name     Subject
1      Adam      French
1      Adam      English 
1      Adam      Science
2      Jon        Maths
2      Jon        French

Output expected:

 StuId     Name       Subjects
  1        Adam       French    English   Science
  2         Jon       Maths     French

Thanks

Anuj
  • 1
  • 1
  • In order to kick start a meaningful conversation, you need to specify how you parse this data and what your internal structure is. – Bogdan Ionitza Jul 25 '22 at 09:10

3 Answers3

2

IIUC, You can use pandas.pivot_table with aggfunc=' '.join.

df = df.pivot_table(index=['StuId','Name'], values=['Subject'], 
                    aggfunc=' '.join).reset_index()
print(df)

   StuId  Name                  Subject
0      1  Adam  French English  Science
1      2   Jon             Maths French
I'mahdi
  • 23,382
  • 5
  • 22
  • 30
0

https://note.nkmk.me/en/python-pandas-t-transpose/

Go through this site. clear answer to Your Question

Baba Ranjith
  • 196
  • 14
0

You can group by the other columns and aggregate the subjects using a list -

 df.groupby(['StuId', 'Name']).agg(lambda x: ' '.join(list(x)))

Output

                           Subject
StuId Name                        
1     Adam  French English Science
2     Jon             Maths French
Mortz
  • 4,654
  • 1
  • 19
  • 35