Search code examples
pythonpandasdata-analysiscryptocurrency

Errors when load selected raw data into dataframe


I want to select three groups of json data and load them into a dataframe but i got the errors "string indices must be integers". Can anyone kindly tell me what is the reason for it?

The code is as following and i have also attached the screenshot:

for currency in data:
if '/BTC' in currency['symbol']:
    change_daily=currency['percentage']
    name=currency["symbol"]
    price = currency['lastPrice']


df_binance.append({"NAME":name,"24h_change":change_daily,"PRICE":price})

enter image description here


Solution

  • Looks like currency is a key in data dictionary here:

    for currency in data:
        if '/BTC' in data[currency]['symbol']:
            change_daily=data[currency]['percentage']
            name=data[currency]["symbol"]
            price = data[currency]['last']
            df_binance.append({"NAME":name,"24h_change":change_daily,"PRICE":price})
    

    BTW you should define df_binance list before your loop. And better iterate with for currency in data.keys()

    Update: Shorter load method:

    df = pd.DataFrame(data).T.reset_index()[['symbol', 'percentage', 'last']]
    df = df.loc[df.symbol.str.endswith("BTC")]