简体   繁体   中英

Python order dataframe alphabetically

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 

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)

您可以使用sort_values方法对数据sort_values进行排序。

df.sort_values('student')

try

df.sort_values(by='student')

or, if you want Z first:

df.sort_values(by='student', ascending=False)

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
在此输入图像描述

testing over larger data
在此输入图像描述

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)

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

df.sort_values(by=['student'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM