【问题标题】:Can you pull an API call straight into a Pandas Dataframe?您可以将 API 调用直接拉入 Pandas Dataframe 吗?
【发布时间】:2020-08-21 07:52:55
【问题描述】:

我正在构建一个小程序来绘制一些 API 数据。我已将其设置为提取数据,然后创建一个本地 json,然后我从中创建一个数据框。

有没有办法跳过本地文件并将数据直接拉入数据框?

如您所见,我正在使用的示例是爱尔兰,但我希望最终得到可以引用任何国家/地区的内容,并希望避免创建文件。

# Import the libraries
import requests
import json
from datetime import datetime

import matplotlib.pyplot as plt

import pandas as pd
import numpy as np
from pandas import Series, DataFrame

# Save the current API call as a JSON file
# countryStatusdDayOne

# 1. Make an API call and store the response.
url = 'https://api.covid19api.com/total/dayone/country/ireland'
data = requests.get(url)

# Store the API response in a variable.
available_data = data.json()

filename = 'data/covid_call__ireland_day_one_workable.json'
with open(filename, 'w') as f:
    json.dump(available_data, f, indent=4)

# read the json
ireland = pd.read_json('data/covid_call__ireland_day_one_workable.json')

# create a dataframe 
df_ire = pd.DataFrame(ireland)

这对我来说都是全新的,所以任何关于如何格式化或改进我的代码的建议也非常欢迎!

【问题讨论】:

  • 我认为没有办法直接将 API 调用修补到 Pandas。唯一的方法是在 API 上执行获取请求,并将 json 传递给pd.read_json

标签: python json pandas api dataframe


【解决方案1】:

IIUC,您可以使用pd.json_normalize 从内存中读取数据,而无需在磁盘上创建文件。

如果您正在阅读多个国家/地区,则可以将所有值保存在字典中。

import requests
import json
from datetime import datetime

import matplotlib.pyplot as plt

import pandas as pd
import numpy as np
from pandas import Series, DataFrame

# Save the current API call as a JSON file
# countryStatusdDayOne

country = 'ireland'

# 1. Make an API call and store the response.
url = f'https://api.covid19api.com/total/dayone/country/{country}'
data = requests.get(url)

# Store the API response in a variable.
available_data = data.json()

dfs = {}

dfs[f'{country}'] = pd.json_normalize(available_data)

print(dfs['ireland'])

    Country CountryCode Province City CityCode Lat Lon  Confirmed  Deaths  \
0   Ireland                                      0   0          1       0   
1   Ireland                                      0   0          1       0   
2   Ireland                                      0   0          1       0   
3   Ireland                                      0   0          2       0   
4   Ireland                                      0   0          6       0   
..      ...         ...      ...  ...      ...  ..  ..        ...     ...   
62  Ireland                                      0   0      20833    1265   
63  Ireland                                      0   0      21176    1286   
64  Ireland                                      0   0      21506    1303   
65  Ireland                                      0   0      21772    1319   
66  Ireland                                      0   0      21983    1339   

    Recovered  Active                  Date  
0           0       1  2020-02-29T00:00:00Z  
1           0       1  2020-03-01T00:00:00Z  
2           0       1  2020-03-02T00:00:00Z  
3           0       2  2020-03-03T00:00:00Z  
4           0       6  2020-03-04T00:00:00Z  
..        ...     ...                   ...  
62      13386    6182  2020-05-01T00:00:00Z  
63      13386    6504  2020-05-02T00:00:00Z  
64      13386    6817  2020-05-03T00:00:00Z  
65      13386    7067  2020-05-04T00:00:00Z  
66      13386    7258  2020-05-05T00:00:00Z  

【讨论】:

    【解决方案2】:

    你可以使用json_normalize

    import requests
    import pandas as pd
    from pandas.io.json import json_normalize
    
    
    url = 'https://api.covid19api.com/total/dayone/country/ireland'
    data = requests.get(url)
    
    # Store the API response in a variable.
    available_data = data.json()
    df = json_normalize(available_data)
    print(df.head())
    

    【讨论】:

    • 这似乎有效,但我得到了这个错误代码:C:\Users\Cormac\Anaconda3\lib\site-packages\ipykernel_launcher.py:11: FutureWarning: pandas.io.json.json_normalize已弃用,请使用 pandas.json_normalize
    • @CormacDaly 这只是一个警告而不是错误,如果您使用的是最新版本的pandas v1.0.1,那么您可以以from pandas import json_normalize 的方式导入它,这应该会删除该警告跨度>
    • 谢谢。我已经实施了您的建议,并且效果很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-01
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多