How to Sort data by Column in a CSV File in Python ?

06 Jun, 2021
In this article, we will discuss how to sort CSV by column(s) using Python.
Method 1: Using sort_values()
We can take the header name as per our requirement, the axis can be either 0 or 1, where 0 means ‘rows’ and ‘1’ means ‘column’. Ascending can be either True/False and if True, it gets arranged in ascending order, if False, it gets arranged in descending order.
Syntax: DataFrame.sort_values(by, axis=0, ascending=True, inplace=False, kind=’quicksort’, na_position=’last’)
CSV File Used:
Below are various which depict various ways to sort a CSV dataset.
Example 1: Sorting the dataset in descending order on the basis of salary
Python3
# importing pandas packageimport pandas as pandasForSortingCSV  # assign datasetcsvData = pandasForSortingCSV.read_csv("sample.csv")                                         # displaying unsorted data frameprint("\nBefore sorting:")print(csvData)  # sort data framecsvData.sort_values(["Salary"],                     axis=0,                    ascending=[False],                     inplace=True)  # displaying sorted data frameprint("\nAfter sorting:")print(csvData)
Output:
Example 2: Sorting the dataset in default (ascending) order on the basis of salary.
Python3
# importing pandas packageimport pandas as pandasForSortingCSV  # assign datasetcsvData = pandasForSortingCSV.read_csv("sample.csv")                                         # displaying unsorted data frameprint("\nBefore sorting:")print(csvData)  # sort data framecsvData.sort_values(csvData.columns[4],                     axis=0,                    inplace=True)  # displaying sorted data frameprint("\nAfter sorting:")print(csvData)
Output:
Example 3: Sorting the dataset on the basis of Name, Age and, Height in ascending order.
Python3
# importing pandas packageimport pandas as pandasForSortingCSV  # assign datasetcsvData = pandasForSortingCSV.read_csv("sample.csv")                                         # displaying unsorted data frameprint("\nBefore sorting:")print(csvData)  # sort data framecsvData.sort_values(["Name", "Age", "Height"],                     axis=0,                    ascending=[True, True, True],                     inplace=True)  # displaying sorted data frameprint("\nAfter sorting:")print(csvData)
Output:
Example 4: Sorting the dataset on the basis of Salary in descending order and Age in ascending order.
Python3
# importing pandas packageimport pandas as pandasForSortingCSV  # assign datasetcsvData = pandasForSortingCSV.read_csv("sample.csv")  # displaying unsorted data frameprint("\nBefore sorting:")print(csvData)  # sort data framecsvData.sort_values([csvData.columns[4], csvData.columns[2]],                    axis=0,                    ascending=[False, True],                    inplace=True)  # displaying sorted data frameprint("\nAfter sorting:")print(csvData)
Output:
Method 2: Using sorted()
Another way of sorting CSV files is by using the sorted() method on the CSV module object. However, it can only sort CSV files based on only one column.
Syntax: 
sorted(iterable, key, reverse)
Below are various which depict various ways to sort a CSV dataset.
Example 1: Sorting the dataset in ascending order on the basis of Age.
Python3
# import modules import csv ,operator  # load csv filedata = csv.reader(open('sample.csv'),delimiter=',')  # sort data on the basis of agedata = sorted(data, key=operator.itemgetter(2))      # displaying sorted data print('After sorting:')display(data)
Output:
Example 2: Sorting the dataset in descending order on the basis of Age.
Python3
# import modules import csv ,operator  # load csv filedata = csv.reader(open('sample.csv'),delimiter=',')  # sort data on the basis of agedata = sorted(data, key=operator.itemgetter(2), reverse=True)      # displaying sorted data print('After sorting:')display(data)
Output:
Picked
python-csv
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.