如何将所有 CSV 文件合并到单个 Pandas DataFrame 中 - Python?
要合并所有 CSV 文件,请使用 GLOB 模块。在 **concat()** 中使用 os.path.join() 方法 将 CSV 文件合并在一起。以下是一些我们可以用来将多个 CSV 文件合并到单个 DataFrame 中的常用方法 -
-
os.path.join() 和 glob
-
使用 glob 模式合并 CSV 文件
-
按特定顺序合并 CSV 文件
使用 'os.path.join()' 和 glob
首先,导入所需的库。我们将 pd 作为 Python Pandas 库的别名。
import pandas as pd import glob import os
此方法通过连接目录路径和文件名模式来构造文件路径。
示例
在下面的示例代码中,**glob.glob()** 返回与给定模式 (“sales”) 匹配的所有文件名的列表。
import pandas as pd
import glob
import os
# setting the path for joining multiple files
files = os.path.join("C:\Users\amit_\Desktop", "sales*.csv")
# list of merged files returned
files = glob.glob(files)
print("Resultant CSV after joining all CSV files at a particular location...");
# joining files with concat and read_csv
df = pd.concat(map(pd.read_csv, files), ignore_index=True)
print(df)
Sales1.csv
| 汽车 | 地点 | 销量 | |
|---|---|---|---|
| 0 | 奥迪 | 班加罗尔 | 80 |
| 1 | 保时捷 | 孟买 | 110 |
| 2 | 劳斯莱斯 | 浦那 | 100 |
Sales2.csv
| 汽车 | 地点 | 销量 | |
|---|---|---|---|
| 3 | 宝马 | 德里 | 95 |
| 4 | 梅赛德斯 | 海德拉巴 | 80 |
| 5 | 兰博基尼 | 昌迪加尔 | 80 |
Sales3.csv
| 汽车 | 地点 | 销量 | |
|---|---|---|---|
| 6 | 沃尔沃 | 拉贾斯坦邦 | 150 |
| 7 | 现代 | 曼尼普尔邦 | 120 |
| 8 | 丰田 | 喜马偕尔邦 | 70 |
输出
将所有 CSV 文件连接到特定位置后的结果 CSV 文件。
| 汽车 | 地点 | 销量 | |
|---|---|---|---|
| 0 | 奥迪 | 班加罗尔 | 80 |
| 1 | 保时捷 | 孟买 | 110 |
| 2 | 劳斯莱斯 | 浦那 | 100 |
| 3 | 宝马 | 德里 | 95 |
| 4 | 梅赛德斯 | 海德拉巴 | 80 |
| 5 | 兰博基尼 | 昌迪加尔 | 80 |
| 6 | 沃尔沃 | 拉贾斯坦邦 | 150 |
| 7 | 现代 | 曼尼普尔邦 | 120 |
| 8 | 丰田 | 喜马偕尔邦 | 70 |
使用 glob 模式合并 CSV 文件
当我们确定文件位置时,可以直接使用文件路径,而无需使用 **os.path.join()**,路径硬编码到 **glob** 方法中。
示例
在下面的示例中,列表推导式 **[pd.read_csv(f) for f in files]** 读取每个 CSV 文件,然后将其传递给 **pd.concat()** 以进行合并。
import pandas as pd
import glob
# Fetch all CSV files directly from the path
files = glob.glob("C:\Users\amit_\Desktop\sales*.csv")
# Concatenate them into a single DataFrame
df = pd.concat([pd.read_csv(f) for f in files], ignore_index=True)
# Display the final DataFrame
print(df)
输出
| 汽车 | 地点 | 销量 | |
|---|---|---|---|
| 0 | 奥迪 | 班加罗尔 | 80 |
| 1 | 保时捷 | 孟买 | 110 |
| 2 | 劳斯莱斯 | 浦那 | 100 |
| 3 | 宝马 | 德里 | 95 |
| 4 | 梅赛德斯 | 海德拉巴 | 80 |
| 5 | 兰博基尼 | 昌迪加尔 | 80 |
| 6 | 沃尔沃 | 拉贾斯坦邦 | 150 |
| 7 | 现代 | 曼尼普尔邦 | 120 |
| 8 | 丰田 | 喜马偕尔邦 | 70 |
按特定顺序合并 CSV 文件
如果我们想按 **特定顺序** 合并文件,可以在连接之前对文件名列表进行排序。在下面的示例中,**file.sort()** 函数根据命名按字母或数字顺序对文件进行排序。
import pandas as pd
import glob
import os
# Fetch all CSV files matching the pattern
files = glob.glob("C:\Users\amit_\Desktop\sales*.csv")
# Sort files alphabetically or numerically (depending on naming)
files.sort()
# Concatenate them into a DataFrame
df = pd.concat(map(pd.read_csv, files), ignore_index=True)
# Display the final DataFrame
print(df)
输出
| 汽车 | 地点 | 销量 | |
|---|---|---|---|
| 0 | 奥迪 | 班加罗尔 | 80 |
| 1 | 保时捷 | 孟买 | 110 |
| 2 | 劳斯莱斯 | 浦那 | 100 |
| 3 | 宝马 | 德里 | 95 |
| 4 | 梅赛德斯 | 海德拉巴 | 80 |
| 5 | 兰博基尼 | 昌迪加尔 | 80 |
| 6 | 沃尔沃 | 拉贾斯坦邦 | 150 |
| 7 | 现代 | 曼尼普尔邦 | 120 |
| 8 | 丰田 | 喜马偕尔邦 | 70 |
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP