【问题标题】:Binance candles to pandas dataframe币安蜡烛到熊猫数据框
【发布时间】:2018-04-23 12:13:00
【问题描述】:
from binance.client import Client
import pandas as pd


#Binance Api data
api_key = 'hidden'
api_secret = 'hidden'


#connect Binance
client = Client(api_key, api_secret)


#klines/candlesticks
candles = client.get_klines(symbol='BTCUSDT', interval=Client.KLINE_INTERVAL_1MINUTE)

#create dataframe for candles
candles_dataframe = pd.DataFrame(columns= ['Open time', 'Open', 'High', 'Low', 'Close', 'Volume', 'Close time', \
            'Quote asset volume', 'Number of trades', 'Taker buy base asset volume', 'Taker buy quote asset volume', \
             'Can be ignored'])

candle0 = candles[0]
candles_dataframe.append(candle0, ignore_index=True)
print(candles_dataframe)

所以蜡烛是一个列表,它返回数据框列中描述的值:

[1524425400000, '8918.00000000', '8918.01000000', '8911.07000000', 
'8913.94000000', '9.39563900', 1524425459999, '83771.29790726', 78, 
'6.44918600', '57506.87361929', '0']

我明白了

/Volumes/Data/Dropbox/Dropbox/Coding/btc_forecast/venv/lib/python3.6/site-packages/pandas/core/indexes/api.py:77: RuntimeWarning: '<' not supported between instances of 'str' and 'int', sort order is undefined for incomparable objects`
result = result.union(other)

我的数据框是空的。 我该怎么办?

【问题讨论】:

    标签: python list pandas


    【解决方案1】:

    我使用 numpy 的解决方案

    ```

    from binance.client import Client
    import pandas as pd
    import numpy as np
    
    client = Client('', '')
    def getCandles():
        df = pd.DataFrame(columns= ['Open_time', 'Open', 'High', 'Low', 'Close', 'Volume', 'Close_time'])
        candles = client.get_klines(symbol='BTCUSDT', interval=Client.KLINE_INTERVAL_1MINUTE)
    
        opentime, lopen, lhigh, llow, lclose, lvol, closetime = [], [], [], [], [], [], []
    
        for candle in candles:
            opentime.append(candle[0])
            lopen.append(candle[1])
            lhigh.append(candle[2])
            llow.append(candle[3])
            lclose.append(candle[4])
            lvol.append(candle[5])
            closetime.append(candle[6])
    
        df['Open_time'] = opentime
        df['Open'] = np.array(lopen).astype(np.float)
        df['High'] = np.array(lhigh).astype(np.float)
        df['Low'] = np.array(llow).astype(np.float)
        df['Close'] = np.array(lclose).astype(np.float)
        df['Volume'] = np.array(lvol).astype(np.float)
        df['Close_time'] = closetime
        return df
    

    【讨论】:

      【解决方案2】:

      所以 .loc 起作用了: 蜡烛数据帧.loc[0] = 蜡烛0

      【讨论】:

        【解决方案3】:
        from binance.client import Client
        import pandas as pd
        
        
        #Binance Api data
        api_key = 'hidden'
        api_secret = 'hidden'
        
        
        #connect Binance
        client = Client(api_key, api_secret)
        
        
        #klines/candlesticks
        candles = client.get_klines(symbol='BTCUSDT', interval=Client.KLINE_INTERVAL_1MINUTE)
        
        cd = pd.DataFrame(candles)
        cd.columns = ['Open time', 'Open', 'High', 'Low', 'Close', 'Volume', 'Close time', 'Quote asset volume', 'Number of trades', 'Taker buy base asset volume', 'Taker buy quote asset volume', 'Can be ignored']
        

        这应该可以解决问题

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-06-03
          • 2015-11-12
          • 2014-02-01
          • 1970-01-01
          • 2018-05-03
          • 1970-01-01
          相关资源
          最近更新 更多