我想从一个目录中读取几个 CSV 文件到 pandas 中,并将它们连接到一个大 DataFrame 中。我一直无法弄清楚。这是我目前所拥有的:
import glob
import pandas as pd
# Get data file names
path = r'C:\DRO\DCL_rawdata_files'
filenames = glob.glob(path + "/*.csv")
dfs = []
for filename in filenames:
dfs.append(pd.read_csv(filename))
# Concatenate all data into one DataFrame
big_frame = pd.concat(dfs, ignore_index=True)
我想我在 for 循环中需要一些帮助?
最佳答案
见 pandas: IO tools对于所有可用的 .read_
方法。
如果所有 CSV 文件都具有相同的列,请尝试以下代码。
我已经添加了header=0
,这样在读取CSV文件的第一行之后,就可以将其分配为列名了。
import pandas as pd
import glob
import os
path = r'C:\DRO\DCL_rawdata_files' # use your path
all_files = glob.glob(os.path.join(path , "/*.csv"))
li = []
for filename in all_files:
df = pd.read_csv(filename, index_col=None, header=0)
li.append(df)
frame = pd.concat(li, axis=0, ignore_index=True)
或者,归因于来自 Sid 的评论.
all_files = glob.glob(os.path.join(path, "*.csv"))
df = pd.concat((pd.read_csv(f) for f in all_files), ignore_index=True)
- 通常需要识别每个数据样本,这可以通过向数据框中添加新列来完成。
-
pathlib
本示例将使用标准库中的。它将路径视为具有方法的对象,而不是要切片的字符串。
导入和设置
from pathlib import Path
import pandas as pd
import numpy as np
path = r'C:\DRO\DCL_rawdata_files' # or unix / linux / mac path
# Get the files from the path provided in the OP
files = Path(path).glob('*.csv') # .rglob to get subdirectories
选项 1:
- 使用文件名添加一个新列
dfs = list()
for f in files:
data = pd.read_csv(f)
# .stem is method for pathlib objects to get the filename w/o the extension
data['file'] = f.stem
dfs.append(data)
df = pd.concat(dfs, ignore_index=True)
选项 2:
- 使用
enumerate
添加具有通用名称的新列
dfs = list()
for i, f in enumerate(files):
data = pd.read_csv(f)
data['file'] = f'File {i}'
dfs.append(data)
df = pd.concat(dfs, ignore_index=True)
选项 3:
- 使用列表解析创建数据框,然后使用
np.repeat
添加新列。[f'S{i}' for i in range(len(dfs))]
创建一个字符串列表来命名每个数据帧。[len(df) for df in dfs]
创建一个长度列表
- 此选项归属于该绘图 answer .
# Read the files into dataframes
dfs = [pd.read_csv(f) for f in files]
# Combine the list of dataframes
df = pd.concat(dfs, ignore_index=True)
# Add a new column
df['Source'] = np.repeat([f'S{i}' for i in range(len(dfs))], [len(df) for df in dfs])
选项 4:
df = pd.concat((pd.read_csv(f).assign(filename=f.stem) for f in files), ignore_index=True)
或
df = pd.concat((pd.read_csv(f).assign(Source=f'S{i}') for i, f in enumerate(files)), ignore_index=True)
关于python - 将多个 CSV 文件导入 pandas 并连接到一个 DataFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20906474/