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

时间:2019-01-21 05:06:31

标签: python python-3.x pandas dataframe

我一直试图在四个循环中附加DataFrame,for循环工作正常,但是它没有附加数据帧,任何帮助将不胜感激。

   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

3 个答案:

答案 0 :(得分:0)

移动以下代码

b = pd.DataFrame()

到循环外部将解决您的问题。现在,每个循环将'b'重新初始化为空数据帧。

答案 1 :(得分:0)

立即问题是您在b循环的每次迭代中将for定义为一个空数据框 。相反,请在您的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)

但是建议在循环中附加数据帧。它需要不必要的复制操作并且效率低下。 recommend在可迭代的数据帧上使用pd.concat的文档:

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)

答案 2 :(得分:0)

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

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")
相关问题