Import multiple csv files into pandas and concatenate into one DataFrame(将多个csv文件导入pandas并拼接成一个DataFrame)
问题描述
我想将目录中的几个 csv 文件读入 pandas 并将它们连接到一个大 DataFrame 中.我一直无法弄清楚.这是我目前所拥有的:
I would like to read several csv files from a directory into pandas and concatenate them into one big DataFrame. I have not been able to figure it out though. Here is what I have so far:
import glob
import pandas as pd
# get data file names
path =r'C:DRODCL_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 循环中需要一些帮助???
I guess I need some help within the for loop???
推荐答案
如果您在所有 csv
文件中都有相同的列,那么您可以尝试下面的代码.我添加了 header=0
以便在读取 csv
后第一行可以指定为列名.
If you have same columns in all your csv
files then you can try the code below.
I have added header=0
so that after reading csv
first row can be assigned as the column names.
import pandas as pd
import glob
path = r'C:DRODCL_rawdata_files' # use your path
all_files = glob.glob(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)
这篇关于将多个csv文件导入pandas并拼接成一个DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将多个csv文件导入pandas并拼接成一个DataFrame
- padding='same' 转换为 PyTorch padding=# 2022-01-01
- 沿轴计算直方图 2022-01-01
- python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令 2022-01-01
- 分析异常:路径不存在:dbfs:/databricks/python/lib/python3.7/site-packages/sampleFolder/data; 2022-01-01
- pytorch 中的自适应池是如何工作的? 2022-07-12
- 如何将一个类的函数分成多个文件? 2022-01-01
- 如何在 Python 的元组列表中对每个元组中的第一个值求和? 2022-01-01
- 如何在 python3 中将 OrderedDict 转换为常规字典 2022-01-01
- 使用Heroku上托管的Selenium登录Instagram时,找不到元素';用户名'; 2022-01-01
- python-m http.server 443--使用SSL? 2022-01-01