Home > Mobile >  Using a function to retrieve keys to put in a dataframe
Using a function to retrieve keys to put in a dataframe

Time:12-25

I am using streamlit and pandas.

I am using a function which returns keys from a API dictionary imported from another .py file. The function does return the keys I want. However when I convert the function into a DataFrame with streamlit I get errors. Can anyone help? I want to show the data represented by the function usdMarket() as a dataframe. The last line returns the data in json format.

import streamlit as st
import pandas as pd
from alt import bncQ

def usdMarket():
    for i in range (0, len(bncQ['symbols']), 1):
        if bncQ['symbols'][i]['quoteAsset']=='USD':
            bncQ['symbols'][i]

st.DataFrame(usdMarket())

CodePudding user response:

It's unclear if you have to use those libraries, but this takes the url provided and parses it down to USD.

import requests
import pandas as pd
data = requests.get('https://api.binance.us/api/v3/exchangeInfo').json()
df = pd.DataFrame(data['symbols'])
dfUSD = df[['symbol', 'quoteAsset']].loc[df['quoteAsset']=='USD']
dfUSD

       symbol quoteAsset
0      BTCUSD        USD
1      ETHUSD        USD
2      XRPUSD        USD
3      BCHUSD        USD
4      LTCUSD        USD
..        ...        ...
127   CTSIUSD        USD
129    DOTUSD        USD
131    YFIUSD        USD
133  1INCHUSD        USD
135    FTMUSD        USD

[67 rows x 2 columns]

CodePudding user response:

st.DataFrame does not convert data into a dataFrame it just gets a dataFrame and presents it in the Streamlit app.

Although you don't show it in the question code you say the last line in the usdMarket function returns the data in json format so you need to convert it to dataFrame for st.DataFrame.

This code should work:

import streamlit as st
import pandas as pd
from alt import bncQ

def usdMarket():
    for i in range (0, len(bncQ['symbols']), 1):
        if bncQ['symbols'][i]['quoteAsset']=='USD':
            bncQ['symbols'][i]
    # return some json

st.DataFrame(pd.read_json(usdMarket()))
  • Related