使用 Python 使用 EIA API 提取数据的问题

问题描述 投票:0回答:1

我正在尝试使用能源信息局的 API 将一些能源数据提取到数据框中。我写了以下代码:

import requests
import pandas as pd

# Set the URL
url = "https://api.eia.gov/v2/electricity/rto/fuel-type-data/data/?frequency=hourly&data[0]=value&facets[respondent][]=ERCO&start=2018-07-01T00&end=2023-04-25T00&sort[0][column]=period&sort[0][direction]=desc&offset=0&length=5000&api_key=7xu8nejTweL67Zzzbsj2K4ngcGgTS828SqpfmSkD"

# Make the API request
response = requests.get(url)

# Extract the data from the response
data = response.json()["series"][0]["data"]

# Convert the data to a pandas dataframe
df = pd.DataFrame(data, columns=["period", "value"])

# Print the dataframe
print(df)

但是,我收到此错误:

KeyError                                  Traceback (most recent call last)
Cell In[27], line 11
      8 response = requests.get(url)
     10 # Extract the data from the response
---> 11 data = response.json()["series"][0]["data"]
     13 # Convert the data to a pandas dataframe
     14 df = pd.DataFrame(data, columns=["period", "value"])

KeyError: 'series'

我检查了所有库是否都已正确安装和导入,因此我对问题的根源感到困惑。知道我做错了什么吗?我将非常感谢您的帮助!

python pandas python-requests
1个回答
0
投票

尝试:

import requests
import pandas as pd

# Set the URL
url = "https://api.eia.gov/v2/electricity/rto/fuel-type-data/data/?frequency=hourly&data[0]=value&facets[respondent][]=ERCO&start=2018-07-01T00&end=2023-04-25T00&sort[0][column]=period&sort[0][direction]=desc&offset=0&length=5000&api_key=7xu8nejTweL67Zzzbsj2K4ngcGgTS828SqpfmSkD"

# Make the API request
data = requests.get(url).json()
df = pd.DataFrame(data['response']['data'])
print(df.head())

打印:

          period respondent                              respondent-name fueltype    type-name  value    value-units
0  2023-04-25T00       ERCO  Electric Reliability Council of Texas, Inc.      WAT        Hydro    162  megawatthours
1  2023-04-25T00       ERCO  Electric Reliability Council of Texas, Inc.      COL         Coal   6398  megawatthours
2  2023-04-25T00       ERCO  Electric Reliability Council of Texas, Inc.      WND         Wind  10020  megawatthours
3  2023-04-25T00       ERCO  Electric Reliability Council of Texas, Inc.      OTH        Other     99  megawatthours
4  2023-04-25T00       ERCO  Electric Reliability Council of Texas, Inc.       NG  Natural gas  17616  megawatthours

如果您只想拥有

period
value
列:

print(df[['period', 'value']].head())

打印:

          period  value
0  2023-04-25T00    162
1  2023-04-25T00   6398
2  2023-04-25T00  10020
3  2023-04-25T00     99
4  2023-04-25T00  17616
© www.soinside.com 2019 - 2024. All rights reserved.