【问题标题】:How to convert api JSON output to a dataframe?如何将 api JSON 输出转换为数据框?
【发布时间】:2020-07-15 18:35:13
【问题描述】:

我正在使用 SDK 从 NOAA API 中提取天气数据。下面是示例代码:

import requests, json
import pandas as pd
from pandas.io.json import json_normalize

from noaa_sdk import noaa
n = noaa.NOAA()
n.points_forecast(40.7314, -73.8656, hourly=False)

示例输出如下:

{'@context':
['https://raw.githubusercontent.com/geojson/geojson-ld/master/contexts/geojson-base.jsonld',
{'wx': 'https://api.weather.gov/ontology#',    'geo':
'http://www.opengis.net/ont/geosparql#',    'unit':
'http://codes.wmo.int/common/unit/',    '@vocab':
'https://api.weather.gov/ontology#'}],  'type': 'Feature',
'geometry': {'type': 'GeometryCollection',   'geometries': [{'type':
'Point', 'coordinates': [-73.8610332, 40.7408918]},    {'type':
'Polygon',
    'coordinates': [[[-73.8730892, 40.7534295],
      [-73.8775823, 40.7317593],
      [-73.8489801, 40.7283524],
      [-73.84448110000001, 40.7500224],
      [-73.8730892, 40.7534295]]]}]},  'properties': {'updated': '2020-04-03T09:30:44+00:00',   'units': 'us',   'forecastGenerator':
'BaselineForecastGenerator',   'generatedAt':
'2020-04-03T14:18:55+00:00',   'updateTime':
'2020-04-03T09:30:44+00:00',   'validTimes':
'2020-04-03T03:00:00+00:00/P7DT4H',   'elevation': {'value': 14.9352,
'unitCode': 'unit:m'},   'periods': [{'number': 1,
    'name': 'Today',
    'startTime': '2020-04-03T10:00:00-04:00',
    'endTime': '2020-04-03T18:00:00-04:00',
    'isDaytime': True,
    'temperature': 53,
    'temperatureUnit': 'F',
    'temperatureTrend': 'falling',
    'windSpeed': '18 mph',
    'windDirection': 'N',
    'icon': 'https://api.weather.gov/icons/land/day/rain,50?size=medium',
    'shortForecast': 'Chance Light Rain',
    'detailedForecast': 'A chance of rain. Cloudy. High near 53, with temperatures falling to around 50 in the afternoon. North wind around
18 mph, with gusts as high as 29 mph. Chance of precipitation is
50%.'}

我需要将上述 JSON 输出转换为数据框,以便将其导出为 CSV 文件。如何实现?

我需要数据框有以下列

    'name': 'Today',
    'startTime': '2020-04-03T10:00:00-04:00',
    'endTime': '2020-04-03T18:00:00-04:00',
    'isDaytime': True,
    'temperature': 53,
    'temperatureUnit': 'F',
    'temperatureTrend': 'falling',
    'windSpeed': '18 mph',
    'windDirection': 'N',
    'icon': 'https://api.weather.gov/icons/land/day/rain,50?size=medium',
    'shortForecast': 'Chance Light Rain',
    'detailedForecast': 'A chance of rain. Cloudy. High near 53, with temperatures falling to around 50 in the afternoon. North wind around
18 mph, with gusts as high as 29 mph. Chance of precipitation is
50%.'

【问题讨论】:

  • 你试过pandas.read_json 吗?然后,您将使用 pandas.DataFrame.to_csv 导出到 CSV
  • OK 将 Dataframe 转换为 CSV 没有问题。我需要知道如何将 JSON 转换为 Dataframe。
  • 你至少可以说出你想要什么列...
  • @SergeBallesta Done 添加了我需要的列

标签: python json python-3.x csv dataframe


【解决方案1】:

好的,我们可以先考虑一下。所有相关字段都在列表中的同一个字典中。这是 DataFrame 的本机条目。

假设您已将 json 加载到 data 变量中,您可以这样做:

df = pd.DataFrame(data['properties']['periods'],
          columns= ['name', 'startTime', 'endTime', 'isDaytime', 'temperature',
            'temperatureUnit', 'temperatureTrend', 'windSpeed', 'windDirection',
            'icon', 'shortForecast', 'detailedForecast'])

它会给出:

    name                  startTime                    endTime  isDaytime  temperature temperatureUnit temperatureTrend windSpeed windDirection                                               icon      shortForecast                                   detailedForecast
0  Today  2020-04-03T10:00:00-04:00  2020-04-03T18:00:00-04:00       True           53               F          falling    18 mph             N  https://api.weather.gov/icons/land/day/rain,50...  Chance Light Rain  A chance of rain. Cloudy. High near 53, with t...

【讨论】:

  • 完美运行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-14
  • 2023-03-31
  • 2021-05-23
  • 2019-08-11
  • 1970-01-01
  • 2019-11-15
  • 1970-01-01
相关资源
最近更新 更多