如何在 Python 中按多个列排序 CSV 文件?
在 Python 中,要按多个列对 CSV 文件排序,我们可以使用 Python Pandas 库提供的 **`sort_values()`** 方法。此方法用于通过将列名作为参数来排序值。一些常见的按多个列排序 CSV 文件的方法如下所示。
-
**`sort_values()`**: 用于按多个列排序 DataFrame。
-
**不使用 `inplace` 的 `sort_values()`**: 用于按多个列排序 DataFrame,而不修改原始 DataFrame。
-
**`sort_index()`**: 用于在按指定列排序后按索引排序。
使用 `sort_values()`
**`sort_values()`** 方法将直接修改原始 DataFrame,通过对多个列进行排序使其更高效。
示例
在下面的示例代码中,**`pd.read_csv()`** 函数将 CSV 文件加载到 Pandas DataFrame 中。通过传递列名列表,使用 **`sort_values()`** 按多列排序。通过设置 **`inplace=True`**,原始 DataFrame 将直接被修改,无需创建新的 DataFrame。
import pandas as pd
# Read the input CSV file
dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesRecords.csv")
# Sort by multiple columns: Reg_Price and Car
dataFrame.sort_values(["Reg_Price", "Car"], axis=0, ascending=True, inplace=True, na_position='first')
print("\nSorted CSV file (according to multiple columns) = \n", dataFrame)
输入 CSV 文件
| 汽车 | 购买日期 | 注册价格 | |
|---|---|---|---|
| 0 | 宝马 | 10/10/2020 | 1000 |
| 1 | 雷克萨斯 | 10/12/2020 | 750 |
| 2 | 奥迪 | 10/17/2020 | 750 |
| 3 | 捷豹 | 10/16/2020 | 1500 |
| 4 | 野马 | 10/19/2020 | 1100 |
| 5 | 兰博基尼 | 10/22/2020 | 1000 |
排序后的 CSV 文件
| 汽车 | 购买日期 | 注册价格 | |
|---|---|---|---|
| 2 | 奥迪 | 10/17/2020 | 750 |
| 1 | 雷克萨斯 | 10/12/2020 | 750 |
| 0 | 宝马 | 10/10/2020 | 1000 |
| 5 | 兰博基尼 | 10/22/2020 | 1000 |
| 4 | 野马 | 10/19/2020 | 1100 |
| 3 | 捷豹 | 10/16/2020 | 1500 |
不使用 `inplace` 的 `sort_values()`
不设置参数 **`inplace=True`** 使用 **`sort_values()`** 方法,将返回一个按指定列排序的新 DataFrame。原始 DataFrame 将保持不变。
示例
在下面的示例代码中,不设置 **`inplace=True`**,使用 **`sort_values()`** 对指定列进行排序,并将结果添加到新的 DataFrame 中。
import pandas as pd
# Read the input CSV file
dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesRecords.csv")
# Sort by multiple columns without modifying the original DataFrame
sortedDataFrame = dataFrame.sort_values(["Reg_Price", "Car"], axis=0, ascending=True, na_position='first')
print("\nSorted CSV file (according to multiple columns) = \n", sortedDataFrame)
排序后的 CSV 文件
| 汽车 | 购买日期 | 注册价格 | |
|---|---|---|---|
| 2 | 奥迪 | 10/17/2020 | 750 |
| 1 | 雷克萨斯 | 10/12/2020 | 750 |
| 0 | 宝马 | 10/10/2020 | 1000 |
| 5 | 兰博基尼 | 10/22/2020 | 1000 |
| 4 | 野马 | 10/19/2020 | 1100 |
| 3 | 捷豹 | 10/16/2020 | 1500 |
使用 `sort_index()`
**`sort_index()`** 方法用于使用其 **索引** 对 DataFrame 进行排序。在按多列排序后,我们还可以使用 **`sort_index()`** 方法按升序或降序排序。
示例
import pandas as pd
# Read the input CSV file
dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesRecords.csv")
# Sort by multiple columns
sortedDataFrame = dataFrame.sort_values(["Reg_Price", "Car"], axis=0, ascending=True, na_position='first')
# Further sort by index (optional)
sortedDataFrame = sortedDataFrame.sort_index()
print("\nSorted CSV file (according to multiple columns and index) = \n", sortedDataFrame)
排序后的 CSV 文件
| 汽车 | 购买日期 | 注册价格 | |
|---|---|---|---|
| 0 | 宝马 | 10/10/2020 | 1000 |
| 1 | 雷克萨斯 | 10/12/2020 | 750 |
| 2 | 奥迪 | 10/17/2020 | 750 |
| 3 | 捷豹 | 10/16/2020 | 1500 |
| 4 | 野马 | 10/19/2020 | 1100 |
| 5 | 兰博基尼 | 10/22/2020 | 1000 |
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP