2

I want to get some part of information from API, but I don't know how to filter data (I want to get only chosen values and don't get values if key don't contain "BTC" string) I'm trying to do something like this:

{"BTC_MINT":{"volume":11.00, "high24":0.002, "low24":0.001},
 "BTC_NOTE":{"volume":11.00, "high24":0.002, "low24":0.001}}

I started with pandas, but I don't know if is it proper way.

link = 'https://poloniex.com/public?command=returnTicker'
with urllib.request.urlopen(link) as rawdata:
    data = rawdata.readall().decode()
data = json.loads(data)
print(data.items())
data = pd.DataFrame([[cur, last, volume, high24, low24] 
                     for cur, d in data.items() 
                     for last, x, x, x, volume, x, x, high24, low24 in d.items()])

Unfortunately, this code don't work. I get following error:

[cur, last, volume, high24, low24] for cur, d, x, w, d, q in data.items() for last, x, x, x, volume, x, x, high24, low24 in d.items()
ValueError: need more than 2 values to unpack

Could someone help and tell me how should I do it?

2 Answers 2

2
df = pd.DataFrame({symbol: {"baseVolume": data[symbol].get("baseVolume"), 
                            "high24hr": data[symbol].get("high24hr"), 
                            "low24hr": data[symbol].get("low24hr")} 
                   for symbol in data}).T
>>> df.head()
          baseVolume    high24hr     low24hr
BTC_1CR   0.00000000  0.00000000  0.00000000
BTC_ABY   0.01968682  0.00000020  0.00000019
BTC_ADN   0.00000000  0.00000000  0.00000000
BTC_ARCH  0.07205024  0.00004813  0.00004693
BTC_BBR   0.19846259  0.00002123  0.00002115

To just get the names in the index starting with BTC, do the following:

>>> df[df.index.str.startswith('BTC')].head()

          baseVolume    high24hr     low24hr
BTC_1CR   0.00000000  0.00000000  0.00000000
BTC_ABY   0.01968682  0.00000020  0.00000019
BTC_ADN   0.00000000  0.00000000  0.00000000
BTC_ARCH  0.07205024  0.00004813  0.00004693
BTC_BBR   0.19846259  0.00002123  0.00002115
Sign up to request clarification or add additional context in comments.

Comments

1

You can just pass your dictionary (data) to pd.Dataframe to create a pandas dataframe. If you want to subset it to only contain columns with the string BTC in it, you can do:

df = pd.DataFrame(data)
new_cols = [x for x in df.columns if x.find('BTC') > -1]
new_df = df[new_cols]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.