熊猫返回空的DataFrame,空的列和空的索引

时间:2020-02-20 05:17:32

标签: python pandas api loops youtube

这是来自在线YouTube教程;如果问题不清楚,请点击这里 https://www.youtube.com/watch?v=3nzk_D6PD20&list=PLqT5wYj8Tan3g8cdTYaCN-n-J4jxN2iDH&index=6

我正在尝试从bitfinex API获取特定集合行情记录的所有adj关闭的数据帧。

import requests
import time
import pandas as pd
import numpy as np

curr_date=round(time.time())
start_date=curr_date*1000 - 14*24*60*60*1000 #getting the latest date

sym_requests=requests.get('https://api.bitfinex.com/v1/symbols') #get all ticker symbols

if sym_requests.status_code == 200:
    symbols = eval(sym_requests.content)

symbols_filtered=[]

for elem in symbols:
    if elem[-3:]=='btc':
        symbols_filtered.append(elem) #put all the tickers that have a btc pair in a list


final_df=pd.DataFrame() #create an empty dataframe


for pair in symbols_filtered: #iterate through the different symbols in list
    time.sleep(3)
    r=requests.get('https://api.bitfinex.com/v2/candles/trade:30m:t'
    +pair.upper()+'/hist?sort=1&start='+str(start_date)
    +'&limit=1000') # retrieve symbol pair data
    if r.status_code ==200:
        res=eval(r.content)
        if len(res)>660: # continue loop if only length of data is more than 660 lines
            df=pd.DataFrame(res) #save data into a dataframe
            df.set_index(0)
            final_df[str(pair)]=df[2] # save close values which are located in df[2] column to the final_df dataframe
final_df.dropna(inplace=True)
print(final_df)

该代码似乎适用于各个代码,但是for循环似乎不适用于symbols_filtered=[]列表中的所有代码,因为我得到以下输出: Empty DataFrame Columns: [] Index: []

Id非常感谢您的帮助,因为这是我在平台上的第一个问题。

1 个答案:

答案 0 :(得分:1)

我发现是条件len(res)>600引起了问题,因为接收数据的长度小于660,请尝试使用较小的值,例如600

我得到的最终输出是这样的:

       ltcbtc    ethbtc    etcbtc    zecbtc    xmrbtc    dshbtc    xrpbtc  \
0    0.007638  0.021456  0.001353  0.007496  0.008132  0.012747  0.000029   
1    0.007594  0.021422  0.001341  0.007435  0.008093  0.012706  0.000029   
2    0.007595  0.021493  0.001343  0.007449  0.008072  0.012668  0.000029   
3    0.007606  0.021556  0.001345  0.007450  0.008125  0.012729  0.000029   
4    0.007599  0.021742  0.001340  0.007428  0.008129  0.012730  0.000029   
..        ...       ...       ...       ...       ...       ...       ...   
596  0.007617  0.027687  0.000951  0.006544  0.008192  0.011229  0.000029   
597  0.007576  0.027622  0.000952  0.006489  0.008094  0.011196  0.000029   
598  0.007554  0.027850  0.000946  0.006515  0.008208  0.011165  0.000029   
599  0.007533  0.027798  0.000944  0.006407  0.008103  0.011269  0.000029   
600  0.007575  0.027713  0.000943  0.006396  0.008116  0.011233  0.000029   

       iotbtc    eosbtc    neobtc  
0    0.000035  0.000472  0.001343  
1    0.000035  0.000471  0.001342  
2    0.000035  0.000471  0.001332  
3    0.000035  0.000471  0.001337  
4    0.000035  0.000476  0.001350  
..        ...       ...       ...  
596  0.000029  0.000454  0.001467  
597  0.000029  0.000452  0.001486  
598  0.000029  0.000453  0.001476  
599  0.000029  0.000453  0.001477  
600  0.000029  0.000451  0.001474  

[601 rows x 10 columns]

为了获得更好的代码质量和功能,请在您的代码中添加一个else语句,其逻辑可能相同或符合您的要求