Python order dataframe alphabetically

Question:

I would like to reorder dataframe by student name.
Does anybody have some suggestions?

df = pd.DataFrame({
        'student': [
            'monica', 'nathalia', 'anastasia', 'marina', 'ema'
        ],

    'grade' : ['excellent', 'excellent', 'good', 'very good', 'good'
    ]
    })

    print (df)

                student   grade

        0       monica    excellent        
        1       nathalia  excellent         
        2       anastasia good        
        3       marina    very good          
        4       ema       good 
Asked By: Sheron

||

Answers:

You can sort a dataframe using the sort_values method.

df.sort_values('student')
Answered By: James

try

df.sort_values(by='student')

or, if you want Z first:

df.sort_values(by='student', ascending=False)
Answered By: qbzenker

pd.DataFrame.sort_values is the obvious pandas choice

However, you can use numpy and reconstruct. This will give you a modest performance boost.

a = df.student.values.astype(str).argsort()
pd.DataFrame(df.values[a], df.index[a], df.columns)

       grade    student
2       good  anastasia
4       good        ema
3  very good     marina
0  excellent     monica
1  excellent   nathalia

testing over small data
enter image description here

testing over larger data
enter image description here

Answered By: piRSquared

Pre pandas 0.17:

# Sort by ascending student name
df.sort('student')
# reverse ascending
df.sort('student', ascending=False)

Pandas 0.17+ (as mentioned in the other answers):

# ascending
df.sort_values('student')
# reverse ascending
df.sort_values('student', ascending=False)
Answered By: sgrg

pandas 0.19.2

df.sort_values(by=['contig', 'pos'], ascending=True)

# where contig and pos are the column names. So, you may change for yours.

Note: Use of inplace is very essential if you want to update the same dataframe. Most of the people run into confusion about when to use/not-use inplace.

If you want to make a new-dataframe.

df_sorted = df.sort_values(by=['contig', 'pos'], inplace=False, ascending=True)
Answered By: everestial007

You can do something similar if you’re reading from a csv file.

df.sort_values(by=['student'])
Answered By: Jan
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.