Reputation: 625
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
Upvotes: 32
Views: 145010
Reputation: 442
You can do something similar if you're reading from a csv file.
df.sort_values(by=['student'])
Upvotes: 1
Reputation: 7255
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)
Upvotes: 13
Reputation: 1240
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)
Upvotes: 56
Reputation: 294546
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
Upvotes: 3
Reputation: 4662
try
df.sort_values(by='student')
or, if you want Z first:
df.sort_values(by='student', ascending=False)
Upvotes: 3
Reputation: 36781
You can sort a dataframe using the sort_values
method.
df.sort_values('student')
Upvotes: 8