使用Pandas为来自多个URL的多个CSV文件创建一个数据框 [英] creating one data frame using Pandas for multiple CSV files from many URL's
问题描述
我想使用Pandas从URL的多个CSV文件中创建1个数据框/结构,并保留初始标题行.
I'd like to create 1 data frame/structure using Pandas from multiple CSV files from URL's, keeping the initial header line.
使用单个URL,所有功能都可以按预期运行:
With a single URL everything works as expected:
df = pd.read_csv('http://www.URL1.csv')
我尝试使用多个URL进行以下操作:
I have attempted the following with multiple URL's:
df = pd.read_csv('http://www.URL1.csv', 'http://www.URL2.csv', ...)
但是,当尝试打印进行测试时,结果间隔了数千行,并且与标准布局相去甚远.由于我是Pandas
的新手,所以很显然我做错了.
However, when attempting to print for testing, the result is spaced out over thousands of lines and is far from the standard layout. Since I am new to Pandas
, it is clear I am doing something wrong.
我希望布局如下:
Header1 Header2 Header3 ...
DATA DATA DATA ...
推荐答案
我认为您需要list comprehension
和urls
的list
,其中输出是DataFrames
的list
.然后使用 concat
进行连接:
I think you need list comprehension
with list
of urls
where output is list
of DataFrames
. Then use concat
for join together:
urls = ['http://www.URL1.csv', 'http://www.URL2.csv']
dfs = [pd.read_csv(url) for url in urls]
df = pd.concat(dfs, ignore_index=True)
这篇关于使用Pandas为来自多个URL的多个CSV文件创建一个数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!