Getting date field from JSON url as pandas DataFrame

Viewed 119

I am trying to bring this API URL into a pandas DataFrame and getting the values but still needing to add the date as a column like the other values:

import pandas as pd
from pandas.io.json import json_normalize
import ssl

ssl._create_default_https_context = ssl._create_unverified_context

df = pd.read_json("https://covidapi.info/api/v1/country/DOM")
df = pd.DataFrame(df['result'].values.tolist())


print (df)

Getting this output:

    confirmed  deaths  recovered
0           0       0          0
1           0       0          0
2           0       0          0
3           0       0          0
4           0       0          0
..        ...     ...        ...
72       1488      68         16
73       1488      68         16
74       1745      82         17
75       1828      86         33
76       1956      98         36
1 Answers

You need to pass the index from your dataframe as well as the data itself:

df = pd.DataFrame(index=df.index, data=df['result'].values.tolist())

The line above creates the same columns, but keeps the original date index from the API call.

Related