我是Python的新手,尝试在通过URL下载多个1行DataFrames后将其组合在一起。我一直在尝试使用pandas pd.concat((,但没有成功。到目前为止,我正确地获得了各个DataFrames,但使用以下脚本组合它们是不起作用的:
import pandas as pd
import time
from time import sleep
import numpy as np
import glob
import pathlib as pl2
count = 0
with open('tickertest12.txt', 'r') as my_file:
newline_break = ""
for readline in my_file:
line_strip = readline.strip()
newline_break += line_strip
#start the counter to pause the loop after x lines
count +=1
try:
urls = "https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=" + line_strip + "&apikey=demo&datatype=csv"
# Using pandas.concat() to append a row
#print(url)
dfs = pd.read_csv(urls,header=0)
big_df = pd.concat(dfs,ignore_index=True)
print("this is big_df")
print(dfs)
except:
print("Invalid Symbol",line_strip)
if count == 3:
sleep(0)
print("pausing for 2 secs")
count = 0
.txt文件是一个股票代码列表:IBM等
如有遗漏或不正确之处,我们将不胜感激。
concat需要两个DF来组合
更改如下
# define the empty dataframe prior to the line '`With open`', below `count=0`
big_df=pd.DataFrame()
big_df = pd.concat([big_df, dfs],ignore_index=True)