如何在Python中的 CSV 文件中按列对数据进行排序?
在本文中,我们将讨论如何使用Python按列对 CSV 进行排序。
方法 1:使用sort_values()
我们可以根据我们的要求取标题名称,轴可以是 0 或 1,其中 0 表示“行”,“1”表示“列”。升序可以是真/假,如果为真,则按升序排列,如果为假,则按降序排列。
Syntax: DataFrame.sort_values(by, axis=0, ascending=True, inplace=False, kind=’quicksort’, na_position=’last’)
使用的 CSV 文件:
下面是各种描述对 CSV 数据集进行排序的各种方法。
示例 1:根据薪水降序对数据集进行排序
Python3
# importing pandas package
import pandas as pandasForSortingCSV
# assign dataset
csvData = pandasForSortingCSV.read_csv("sample.csv")
# displaying unsorted data frame
print("\nBefore sorting:")
print(csvData)
# sort data frame
csvData.sort_values(["Salary"],
axis=0,
ascending=[False],
inplace=True)
# displaying sorted data frame
print("\nAfter sorting:")
print(csvData)
Python3
# importing pandas package
import pandas as pandasForSortingCSV
# assign dataset
csvData = pandasForSortingCSV.read_csv("sample.csv")
# displaying unsorted data frame
print("\nBefore sorting:")
print(csvData)
# sort data frame
csvData.sort_values(csvData.columns[4],
axis=0,
inplace=True)
# displaying sorted data frame
print("\nAfter sorting:")
print(csvData)
Python3
# importing pandas package
import pandas as pandasForSortingCSV
# assign dataset
csvData = pandasForSortingCSV.read_csv("sample.csv")
# displaying unsorted data frame
print("\nBefore sorting:")
print(csvData)
# sort data frame
csvData.sort_values(["Name", "Age", "Height"],
axis=0,
ascending=[True, True, True],
inplace=True)
# displaying sorted data frame
print("\nAfter sorting:")
print(csvData)
Python3
# importing pandas package
import pandas as pandasForSortingCSV
# assign dataset
csvData = pandasForSortingCSV.read_csv("sample.csv")
# displaying unsorted data frame
print("\nBefore sorting:")
print(csvData)
# sort data frame
csvData.sort_values([csvData.columns[4], csvData.columns[2]],
axis=0,
ascending=[False, True],
inplace=True)
# displaying sorted data frame
print("\nAfter sorting:")
print(csvData)
Python3
# import modules
import csv ,operator
# load csv file
data = csv.reader(open('sample.csv'),delimiter=',')
# sort data on the basis of age
data = sorted(data, key=operator.itemgetter(2))
# displaying sorted data
print('After sorting:')
display(data)
Python3
# import modules
import csv ,operator
# load csv file
data = csv.reader(open('sample.csv'),delimiter=',')
# sort data on the basis of age
data = sorted(data, key=operator.itemgetter(2), reverse=True)
# displaying sorted data
print('After sorting:')
display(data)
输出:
示例 2:根据薪水按默认(升序)顺序对数据集进行排序。
蟒蛇3
# importing pandas package
import pandas as pandasForSortingCSV
# assign dataset
csvData = pandasForSortingCSV.read_csv("sample.csv")
# displaying unsorted data frame
print("\nBefore sorting:")
print(csvData)
# sort data frame
csvData.sort_values(csvData.columns[4],
axis=0,
inplace=True)
# displaying sorted data frame
print("\nAfter sorting:")
print(csvData)
输出:
示例 3:根据姓名、年龄和高度按升序对数据集进行排序。
蟒蛇3
# importing pandas package
import pandas as pandasForSortingCSV
# assign dataset
csvData = pandasForSortingCSV.read_csv("sample.csv")
# displaying unsorted data frame
print("\nBefore sorting:")
print(csvData)
# sort data frame
csvData.sort_values(["Name", "Age", "Height"],
axis=0,
ascending=[True, True, True],
inplace=True)
# displaying sorted data frame
print("\nAfter sorting:")
print(csvData)
输出:
示例 4:根据 Salary 降序和 Age 升序对数据集进行排序。
蟒蛇3
# importing pandas package
import pandas as pandasForSortingCSV
# assign dataset
csvData = pandasForSortingCSV.read_csv("sample.csv")
# displaying unsorted data frame
print("\nBefore sorting:")
print(csvData)
# sort data frame
csvData.sort_values([csvData.columns[4], csvData.columns[2]],
axis=0,
ascending=[False, True],
inplace=True)
# displaying sorted data frame
print("\nAfter sorting:")
print(csvData)
输出:
方法 2:使用sorted()
另一种对 CSV 文件进行排序的方法是使用sorted() CSV 上的方法 模块对象。但是,它只能根据一列对 CSV 文件进行排序。
句法:
sorted(iterable, key, reverse)
下面是各种描述对 CSV 数据集进行排序的各种方法。
示例 1:根据年龄对数据集进行升序排序。
蟒蛇3
# import modules
import csv ,operator
# load csv file
data = csv.reader(open('sample.csv'),delimiter=',')
# sort data on the basis of age
data = sorted(data, key=operator.itemgetter(2))
# displaying sorted data
print('After sorting:')
display(data)
输出:
示例 2:根据年龄对数据集进行降序排序。
蟒蛇3
# import modules
import csv ,operator
# load csv file
data = csv.reader(open('sample.csv'),delimiter=',')
# sort data on the basis of age
data = sorted(data, key=operator.itemgetter(2), reverse=True)
# displaying sorted data
print('After sorting:')
display(data)
输出: