2

I want to use the apply function to sort each row of a dataframe df:

ID   Student1    Student2    Student3  
1    A           B           C
2    M           E           F
3    H           A           D          

The code is

import numpy as np 
import pandas as pd
df = pd.DataFrame(data=np.array([[1, 'A', 'B', 'C'], [2, 'M', 'E', 'F'], [3, 'H', 'A', 'D']]), columns=['ID', 'Student1', 'Student2', 'Student3'])
df1 = df.apply(np.sort, axis = 1) 

df1 is a dataframe, not a series object. It looks like this:

ID   Student1    Student2    Student3  
1    A           B           C
2    E           F           M
3    A           D           H          

How can I get a dataframe of the following? Thanks.

ID      
1   [A, B, C]     
2   [E, F, M]
3   [A, D, H] 
Steve P
  • 171
  • 1
  • 9
  • Also if your question been answered by blow , please selected one of them as accept thanks . – BENY Mar 06 '19 at 16:33

2 Answers2

2

This can be done with np.sort without using apply , check : When should I ever want to use pandas apply() in my code?

import numpy as np 
df.iloc[:,1:]=np.sort(df.iloc[:,1:].values,1)
df
Out[463]: 
   ID Student1 Student2 Student3
0   1        A        B        C
1   2        E        F        M
2   3        A        D        H

Then

s = pd.Series(df.iloc[:,1:].values.tolist(),index=df.ID)
s
Out[466]: 
ID
1    [A, B, C]
2    [E, F, M]
3    [A, D, H]
dtype: object
BENY
  • 317,841
  • 20
  • 164
  • 234
0

This works like a charm:

df.set_index(['ID']).agg(list,axis=1).reset_index()
hacker315
  • 1,996
  • 2
  • 13
  • 23