Python 如何将api JSON输出转换为数据帧?

Python 如何将api JSON输出转换为数据帧?,python,json,python-3.x,csv,dataframe,Python,Json,Python 3.x,Csv,Dataframe,我正在使用SDK从NOAAAPI中提取天气数据。下面是示例代码: 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.gith

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

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“,”地理“: 'http://www.opengis.net/ont/geosparql'单位': 'http://codes.wmo.int/common/unit/“,“@vocab”: 'https://api.weather.gov/ontology“}],“类型”:“特征”, “几何体”:{“类型”:“GeometryCollection”,“几何体”:[{“类型”: '点','坐标':[-73.8610332,40.7408918]},{'type': “多边形”, “坐标”:[73.8730892,40.7534295], [-73.8775823, 40.7317593], [-73.8489801, 40.7283524], [-73.84448110000001, 40.7500224], [-73.8730892,40.7534295]]},'属性':{'updated':'2020-04-03T09:30:44+00:00','units':'us','forecastGenerator': “BaselineForecastGenerator”、“generatedAt”: “2020-04-03T14:18:55+00:00”,“更新时间”: “2020-04-03T09:30:44+00:00”,“有效时间”: '2020-04-03T03:00:00+00:00/P7DT4H','高程':{'value':14.9352, 'unitCode':'unit:m'},'periods':[{'number':1, “姓名”:“今天”, “开始时间”:“2020-04-03T10:00:00-04:00”, “结束时间”:“2020-04-03T18:00:00-04:00”, “isDaytime”:对, “温度”:53, “温度单位”:“F”, “温度下降”:“下降”, “风速”:“18英里/小时”, “风向”:“N”, '图标':'https://api.weather.gov/icons/land/day/rain,50?尺寸=中等', “短期预测”:“偶然小雨”, “详细预报”:“有可能下雨。多云。接近53度,下午气温下降到50度左右。北风刮来。” 18英里/小时,阵风高达29英里/小时。降雨的可能性很小 50%.'} 我需要将上面的JSON输出转换为一个数据帧,以便将其导出为CSV文件。如何实现这一点

我需要dataframe具有以下列

    '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加载到数据变量中,则只需执行以下操作:

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...

你试过了吗?然后你会使用导出到CSVOk将数据帧转换为CSV没有问题。我需要知道如何将JSON转换为数据帧。你至少可以说你想要什么列…@SergeBallesta Done添加了我需要的列,效果非常好