如何在Python中的for循环中附加数据帧

如何在Python中的for循环中附加数据帧,python,python-3.x,pandas,dataframe,Python,Python 3.x,Pandas,Dataframe,我一直在尝试在四个循环中附加数据帧,因为循环工作得很好,但是它没有附加数据帧,任何帮助都将不胜感激 symbols = ['MSFT', 'GOOGL', 'AAPL'] apikey = 'CR*****YDA' for s in symbols: print(s) url = "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=%s&apikey=%s"

我一直在尝试在四个循环中附加数据帧,因为循环工作得很好,但是它没有附加数据帧,任何帮助都将不胜感激

   symbols = ['MSFT', 'GOOGL', 'AAPL']
   apikey = 'CR*****YDA'
   for s in symbols:
     print(s)
     url = "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=%s&apikey=%s"  % (s, apikey)
     stockdata = urllib.request.urlopen(url)
     data = stockdata.read().decode()
     js = json.loads(data)
     a = pd.DataFrame(js['Time Series (Daily)']).T
     b = pd.DataFrame()
     print(b)
     b = b.append(a, ignore_index=True)
     print(b)
     print("loop successful")

print("run successfull")
产出:

MSFT
Empty DataFrame
Columns: []
Index: []
     1. open   2. high    3. low  4. close  5. volume
0   107.4600  107.9000  105.9100  107.7100   37427587
1   105.0000  106.6250  104.7600  106.1200   28393015
..       ...       ...       ...       ...        ...
99  109.2700  109.6400  108.5100  109.6000   19662331

[100 rows x 5 columns]
loop successful
GOOGL
Empty DataFrame
Columns: []
Index: []
      1. open    2. high     3. low   4. close 5. volume
0   1108.5900  1118.0000  1099.2800  1107.3000   2244569
1   1087.9900  1100.7000  1083.2600  1099.1200   1244801
..        ...        ...        ...        ...       ...
99  1244.1400  1257.8700  1240.6800  1256.2700   1428992

[100 rows x 5 columns]
loop successful
AAPL
Empty DataFrame
Columns: []
Index: []
     1. open   2. high    3. low  4. close 5. volume
0   157.5000  157.8800  155.9806  156.8200  33751023
1   154.2000  157.6600  153.2600  155.8600  29821160
..       ...       ...       ...       ...       ...
99  217.1500  218.7400  216.3300  217.9400  20525117

[100 rows x 5 columns]
loop successful
run successfull

移动以下代码

b = pd.DataFrame()
跳出循环将解决您的问题。现在,“b”在每个循环中都重新初始化为空数据帧。

直接的问题是,在
for
循环的每个迭代中,您将
b
定义为空数据帧。相反,请在
for
循环开始之前定义一次:

b = pd.DataFrame()
for s in symbols:
    # some code
    a = pd.DataFrame(js['Time Series (Daily)']).T
    b = b.append(a, ignore_index=True)
但不建议在循环中附加数据帧。它需要不必要的复制操作,而且效率低下。在一组数据帧上使用的文档:

list_of_dfs = []
for s in symbols:
    # some code
    list_of_dfs.append(pd.DataFrame(js['Time Series (Daily)']).T)

b = pd.concat(list_of_dfs, ignore_index=True)

问题是您一直在用空数据帧擦除b的值。因此,在for循环之前,必须将b定义为数据帧

symbols = ['MSFT', 'GOOGL', 'AAPL']
apikey = 'CR*****YDA'
b = pd.DataFrame()
for s in symbols:
  print(s)
  url = "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=%s&apikey=%s"  % (s, apikey)
  stockdata = urllib.request.urlopen(url)
  data = stockdata.read().decode()
  js = json.loads(data)
  a = pd.DataFrame(js['Time Series (Daily)']).T
  print(b)
  b = b.append(a, ignore_index=True)
  print(b)
  print("loop successful")

print("run successfull")