【问题标题】:Load json to pandas dataframe with openweather API使用 openweathermAP 将 json 加载到 pandas 数据帧
【发布时间】:2020-07-19 10:03:48
【问题描述】:

我正在尝试从 OpenWeatherMap 转换我的 json 数据,但它一直给我错误。当我在其他教程网站上尝试其他 json 文件时,它工作得非常好。

Json 格式:

{
    "cod": "200",
    "message": 0,
    "cnt": 40,
    "list": [
        {
            "dt": 1586250000,
            "main": {
                "temp": 303.36,
                "feels_like": 306.76,
                "temp_min": 303.36,
                "temp_max": 303.95,
                "pressure": 1006,
                "sea_level": 1006,
                "grnd_level": 1004,
                "humidity": 61,
                "temp_kf": -0.59
            },
            "weather": [{
                "id": 500,
                "main": "Rain",
                "description": "light rain",
                "icon": "10d"
            }],
            "clouds": {
                "all": 97
            },
            "wind": {
                "speed": 1.74,
                "deg": 38
            },
            "rain": {
                "3h": 0.29
            },
            "sys": {
                "pod": "d"
            },
            "dt_txt": "2020-04-07 09:00:00"
        }, {
            "dt": 1586260800,
            "main": {
                "temp": 300.42,
                "feels_like": 303.73,
                "temp_min": 300.42,
                "temp_max": 300.86,
                "pressure": 1008,
                "sea_level": 1008,
                "grnd_level": 1006,
                "humidity": 76,
                "temp_kf": -0.44
            },
            "weather": [{
                "id": 500,
                "main": "Rain",
                "description": "light rain",
                "icon": "10n"
            }],
            "clouds": {
                "all": 83
            },
            "wind": {
                "speed": 2.5,
                "deg": 52
            },
            "rain": {
                "3h": 0.53
            },
            "sys": {
                "pod": "n"
            },
            "dt_txt": "2020-04-07 12:00:00"
        }, {
            "dt": 1586271600,
            "main": {
                "temp": 299.85,
                "feels_like": 303.12,
                "temp_min": 299.85,
                "temp_max": 300.15,
                "pressure": 1010,
                "sea_level": 1010,
                "grnd_level": 1008,
                "humidity": 80,
                "temp_kf": -0.3
            },
            "weather": [{
                "id": 500,
                "main": "Rain",
                "description": "light rain",
                "icon": "10n"
            }],
            "clouds": {
                "all": 62
            },
            "wind": {
                "speed": 2.78,
                "deg": 32
            },
            "rain": {
                "3h": 0.16
            },
            "sys": {
                "pod": "n"
            },
            "dt_txt": "2020-04-07 15:00:00"
        }
    ],
    "city": {
        "id": 1880252,
        "name": "Singapore",
        "coord": {
            "lat": 1.2897,
            "lon": 103.8501
        },
        "country": "SG",
        "population": 3547809,
        "timezone": 28800,
        "sunrise": 1586214152,
        "sunset": 1586257828
    }
}

我用 python 创建了一个函数,通过 api 调用读取 JSON 并使用 pandas 将其转换为数据帧

def _weather():
    url = 'http://api.openweathermap.org/data/2.5/forecast?q=Singapore,{API}'
    res = requests.get(url)
    data = res.json()
    return data

将其读取为 json 后,我尝试将其转换为数据帧,但收到错误

#df = pd.dataFrame(_weather)
df = pd.read_json(_weather)

我错过了什么步骤?

预期的数据帧如图所示, 我正在尝试显示 16 行数据,因为我正在为 16 天/每日预测提取数据。

【问题讨论】:

  • 你能添加错误吗?
  • 除了 Talha 的问题,请发布您预期的输出数据框
  • @sammywemmy,我已经添加了链接和图片

标签: python json pandas


【解决方案1】:

试试这个,了解更多关于 json_normalize,

def _weather_pd(url):
    df1 = json_normalize(url['list'], 'weather')
    df2 = json_normalize(url['list'])
    df = df2.drop('weather', axis=1).join(df1)
    return df

【讨论】:

    【解决方案2】:

    只是问题的另一个变体,在处理嵌套数据时,我更喜欢glomjmespath,尤其是json:

    天气部分的路径是:list key -> 'list container' -> weather key -> 'list container'

    列表以 [] 符号标识,而字典键以点 (.) 为前缀

    import jmespath
    expression = jmespath.compile('list[].weather[]')
    res = expression.search(data)
    
    pd.DataFrame(res)
    
        id  main    description icon
    0   500 Rain    light rain  10d
    1   500 Rain    light rain  10n
    2   500 Rain    light rain  10n
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-08
      • 2023-04-10
      • 1970-01-01
      • 2020-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-19
      相关资源
      最近更新 更多