Home > Blockchain >  Dataframe overwrite first result
Dataframe overwrite first result

Time:12-09

I would like to create a dataframe imported from json, which I would later transfer to Excel. The problem is that (df1) write new results over the first result (so basicaly all the results are deleted in the end except the last one). How can I fix it to get all the data?

import json
import requests
import pandas as pd

r = requests.get('https://api.gateio.la/api2/1/feelist')
lista = (r.json())
for key, ad in lista.items():
    sy  = ad['symbol']
    no  = ad['no']

    df0 = [[sy]]
    df1 = pd.DataFrame((df0), index=[no])

print(df1)

My results:

1981 SFM

Desided:

1 GT 2 CNYX 3 USDT ... 1981 SFM

CodePudding user response:

You're rewriting df1 each for-loop iteration. Instead, try to leverage pd.DataFrame constructor:

import json
import requests
import pandas as pd

data = requests.get("https://api.gateio.la/api2/1/feelist").json()


df = pd.DataFrame(
    [(ad["symbol"], ad["no"]) for _, ad in data.items()],
    columns=["Col1", "Col2"],
)
print(df)

Prints:

            Col1  Col2
0             GT     1
1           CNYX     2
2           USDT     3
3       USDT_ETH     4

...

1977         BTL  1978
1978    LIQUIDUS  1979
1979       DEHUB  1980
1980         SFM  1981
  • Related