Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Dataframe overwrite first result

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Desided:

1 GT
2 CNYX
3 USDT

1981 SFM

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading