user20438916
user20438916

Reputation: 1

How can I get a Pandas Dataframe out of the Codeforces API?

I would like to use the CODEFORCERS API for some Analytics (in Python / SQL / even XLS...). I tried to get a sufficient Pandas Dataframe but I get a Dataframe with 0 Rows and 13644 columns. I have no clue how to get a usable Dataframe out of the API.

What I want to do with the data: Analyse different aspects like scores / participants / score changes / rounds ...

Just pulling the data into an XLS sheet / SQL should work as well.

Best, Kiki

I tried

import pandas as pd from sklearn import datasets

contest_list = pd.read_csv("https://codeforces.com/api/contest.list?gym=false") pd.DataFrame(contest_list)

but got a Dataframe with 0 rows × 13644 columns.

Upvotes: 0

Views: 46

Answers (1)

gvee
gvee

Reputation: 17171

My earlier answer was wrong. I made an assumption that the data on your URL was in CSV format (and that Pandas wouldn't support reading from an HTTP endpoint on read_csv... which it does!).

The data on that URL is not in CSV format.

It is in JSON format.

Therefore:

contest_list = pd.read_json("https://codeforces.com/api/contest.list?gym=false")
pd.DataFrame(contest_list)

However this results in: [0 rows x 13652 columns]

This is because the JSON is a complex object that needs a little bit of pre-parsing to make for a nice dataframe

import requests

url = "https://codeforces.com/api/contest.list?gym=false"
response = requests.get(url)

contents = response.json()
results = contents.get("result")
df = pd.DataFrame(results)
print(df)

This results in: [1667 rows x 8 columns]

Upvotes: 0

Related Questions