我是 Python 新手,并嘗試在通過 URL 下載每個資料幀后組合多個 1 行資料幀。我一直在嘗試使用 pandas pd.concat() 沒有成功。到目前為止,我正確地獲得了各個 DataFrame,但是使用以下腳本將它們組合起來不起作用:
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 等。
非常感謝您就上述缺失或不正確的內容提供幫助。
uj5u.com熱心網友回復:
你需要兩個 DF 來合并 concat
更改如下
# 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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/520951.html
