How to sort pandas dataframe by one column
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Sorting a pandas DataFrame by a specific column is a fundamental operation for data manipulation, allowing you to organize your data to facilitate analysis and extraction of insights. In this article, we will explore how to sort a pandas DataFrame using the built-in sorting methods, with technical explanations, examples, and tips to optimize your data sorting process.
Introduction to pandas DataFrame Sorting
The pandas library in Python provides powerful tools for data manipulation, and sorting is one of the primary operations you might want to perform when working with data sets. Sorting helps in organizing your data in ascending or descending order based on one or more columns.
Why Sort Data?
- Organize Data: Sorted data is more understandable and can reveal trends or patterns.
- Data Analysis: Certain analyses depend on the order of data (e.g., finding rank, percentile).
- Ease of Access: Makes access to specific records faster.
Sorting a DataFrame
The sort_values()
method is the primary tool for sorting data in pandas. It sorts a DataFrame by the values of one or more columns.
Basic Syntax
by: Name or list of names to sort by.axis: The axis to sort along. Default is0(row-wise).ascending: Sort ascending vs. descending. Boolean or list of booleans. Default isTrue.inplace: IfTrue, performs operation in place.kind: Choice of sorting algorithm (‘quicksort’,‘mergesort’,‘heapsort’,‘stable’).na_position:‘first’puts NaNs at the beginning,‘last’puts NaNs at the end.
1 Bob 19 92 2 Charlie 23 85 0 Alice 24 88 3 David 34 99
3 David 34 99 1 Bob 19 92 0 Alice 24 88 2 Charlie 23 85
2 Charlie 23.0 NaN 0 Alice 24.0 88.0 1 Bob NaN 92.0 3 David 34.0 99.0
1 Bob 19 92 2 Charlie 23 85 0 Alice 24 88 3 David 34 99
- Algorithm Choice: The default algorithm
quicksortis usually the fastest, but if you need a stable sort (where the order of equal elements is preserved), usemergesort. - Memory Usage: Sorting can be memory-intensive, so handle large datasets with care.
Related reading
- How to specify the prior probability for scikit-learn's Naive Bayes
- How to split a dataframe string column into two columns?
- how to split a dataset into training and validation set keeping ratio between classes?
- How to split data based on a column value in sklearn
- How to sort the letters in a string alphabetically in Python
- How to sort three variables using at most two swaps?
- How to sort with lambda in Python
- How to source virtualenv activate in a Bash script

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.