如何遍历 JSON 响应并附加到数据帧中?

时间:2021-06-17 16:51:20

标签: python pandas api weatherstack

我正在使用 Wea​​therstack API 以一小时为间隔获取历史降水数据。响应是一个嵌套字典。

输入:

params1 = {
    'access_key': 'key',
    'query': '46.02309767625444, -73.43427142273022',
    'historical_date_start' : '2017-01-06',
    'historical_date_end' : '2017-03-04',
    'hourly': '1',
    'interval': '1'
}

api_result1 = requests.get('https://api.weatherstack.com/historical', params1)

api_response1 = api_result1.json()

api_response1
{'request': {'type': 'LatLon',
  'query': 'Lat 46.02 and Lon -73.43',
  'language': 'en',
  'unit': 'm'},
 'location': {'name': 'Joliette',
  'country': 'Canada',
  'region': 'Quebec',
  'lat': '46.033',
  'lon': '-73.433',
  'timezone_id': 'America/Toronto',
  'localtime': '2021-06-17 12:08',
  'localtime_epoch': 1623931680,
  'utc_offset': '-4.0'},
 'current': {'observation_time': '04:08 PM',
  'temperature': 20,
  'weather_code': 113,
  'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png'],
  'weather_descriptions': ['Sunny'],
  'wind_speed': 28,
  'wind_degree': 290,
  'wind_dir': 'WNW',
  'pressure': 1012,
  'precip': 0,
  'humidity': 38,
  'cloudcover': 0,
  'feelslike': 20,
  'uv_index': 5,
  'visibility': 10,
  'is_day': 'yes'},
 'historical': {'2017-01-06': {'date': '2017-01-06',
   'date_epoch': 1483660800,
   'astro': {'sunrise': '08:35 AM',
    'sunset': '05:25 PM',
    'moonrise': '01:10 PM',
    'moonset': '01:34 AM',
    'moon_phase': 'First Quarter',
    'moon_illumination': 56},
   'mintemp': -20,
   'maxtemp': -10,
   'avgtemp': -14,
   'totalsnow': 0.1,
   'sunhour': 8.7,
   'uv_index': 1,
   'hourly': [{'time': '0',
     'temperature': -13,
     'wind_speed': 8,
     'wind_degree': 235,
     'wind_dir': 'SW',
     'weather_code': 329,
     'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0036_cloudy_with_heavy_snow_night.png'],
     'weather_descriptions': ['Patchy moderate snow'],
     'precip': 0.1,
     'humidity': 97,
     'visibility': 7,
     'pressure': 1012,
     'cloudcover': 56,
     'heatindex': -13,
     'dewpoint': -13,
     'windchill': -19,
     'windgust': 17,
     'feelslike': -19,
     'chanceofrain': 0,
     'chanceofremdry': 0,
     'chanceofwindy': 0,
     'chanceofovercast': 0,
     'chanceofsunshine': 0,
     'chanceoffrost': 0,
     'chanceofhightemp': 0,
     'chanceoffog': 0,
     'chanceofsnow': 0,
     'chanceofthunder': 0,
     'uv_index': 1},
    {'time': '100',
     'temperature': -13,
     'wind_speed': 8,
     'wind_degree': 238,
     'wind_dir': 'WSW',
     'weather_code': 329,
     'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0036_cloudy_with_heavy_snow_night.png'],
     'weather_descriptions': ['Patchy moderate snow'],
     'precip': 0,
     'humidity': 97,
     'visibility': 8,
     'pressure': 1013,
     'cloudcover': 52,
     'heatindex': -13,
     'dewpoint': -13,
     'windchill': -19,
     'windgust': 17,
     'feelslike': -19,
     'chanceofrain': 0,
     'chanceofremdry': 0,
     'chanceofwindy': 0,
     'chanceofovercast': 0,
     'chanceofsunshine': 0,
     'chanceoffrost': 0,
     'chanceofhightemp': 0,
     'chanceoffog': 0,
     'chanceofsnow': 0,
     'chanceofthunder': 0,
     'uv_index': 1},
    {'time': '200',
     'temperature': -13,
     'wind_speed': 9,
     'wind_degree': 241,
     'wind_dir': 'WSW',
     'weather_code': 323,
     'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0027_light_snow_showers_night.png'],
     'weather_descriptions': ['Patchy light snow'],
     'precip': 0,
     'humidity': 97,
     'visibility': 9,
     'pressure': 1013,
     'cloudcover': 48,
     'heatindex': -13,
     'dewpoint': -13,
     'windchill': -18,
     'windgust': 17,
     'feelslike': -18,
     'chanceofrain': 0,
     'chanceofremdry': 0,
     'chanceofwindy': 0,
     'chanceofovercast': 0,
     'chanceofsunshine': 0,
     'chanceoffrost': 0,
     'chanceofhightemp': 0,
     'chanceoffog': 0,
     'chanceofsnow': 0,
     'chanceofthunder': 0,
     'uv_index': 1},
    {'time': '300',
     'temperature': -13,
     'wind_speed': 9,
     'wind_degree': 244,
     'wind_dir': 'WSW',
     'weather_code': 323,
     'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0027_light_snow_showers_night.png'],
     'weather_descriptions': ['Patchy light snow'],
     'precip': 0,
     'humidity': 97,
     'visibility': 10,
     'pressure': 1014,
     'cloudcover': 44,
     'heatindex': -13,
     'dewpoint': -13,
     'windchill': -18,
     'windgust': 17,
     'feelslike': -18,
     'chanceofrain': 0,
     'chanceofremdry': 0,
     'chanceofwindy': 0,
     'chanceofovercast': 0,
     'chanceofsunshine': 0,
     'chanceoffrost': 0,
     'chanceofhightemp': 0,
     'chanceoffog': 0,
     'chanceofsnow': 0,
     'chanceofthunder': 0,
     'uv_index': 1},
    {'time': '400',
     'temperature': -14,
     'wind_speed': 8,
     'wind_degree': 247,
     'wind_dir': 'WSW',
     'weather_code': 323,
     'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0027_light_snow_showers_night.png'],
     'weather_descriptions': ['Patchy light snow'],
     'precip': 0,
     'humidity': 97,
     'visibility': 10,
     'pressure': 1014,
     'cloudcover': 35,
     'heatindex': -14,
     'dewpoint': -14,
     'windchill': -19,
     'windgust': 16,
     'feelslike': -19,
     'chanceofrain': 0,
     'chanceofremdry': 0,
     'chanceofwindy': 0,
     'chanceofovercast': 0,
     'chanceofsunshine': 0,
     'chanceoffrost': 0,
     'chanceofhightemp': 0,
     'chanceoffog': 0,
     'chanceofsnow': 0,
     'chanceofthunder': 0,
     'uv_index': 1},
    {'time': '500',
     'temperature': -15,
     'wind_speed': 7,
     'wind_degree': 250,
     'wind_dir': 'WSW',
     'weather_code': 323,
     'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0027_light_snow_showers_night.png'],
     'weather_descriptions': ['Patchy light snow'],
     'precip': 0,
     'humidity': 97,
     'visibility': 10,
     'pressure': 1015,
     'cloudcover': 27,
     'heatindex': -15,
     'dewpoint': -15,
     'windchill': -20,
     'windgust': 14,
     'feelslike': -20,
     'chanceofrain': 0,
     'chanceofremdry': 0,
     'chanceofwindy': 0,
     'chanceofovercast': 0,
     'chanceofsunshine': 0,
     'chanceoffrost': 0,
     'chanceofhightemp': 0,
     'chanceoffog': 0,
     'chanceofsnow': 0,
     'chanceofthunder': 0,
     'uv_index': 1},
    {'time': '600',
     'temperature': -16,
     'wind_speed': 6,
     'wind_degree': 253,
     'wind_dir': 'WSW',
     'weather_code': 323,
     'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0011_light_snow_showers.png'],
     'weather_descriptions': ['Patchy light snow'],
     'precip': 0,
     'humidity': 97,
     'visibility': 10,
     'pressure': 1016,
     'cloudcover': 19,
     'heatindex': -16,
     'dewpoint': -16,
     'windchill': -22,
     'windgust': 13,
     'feelslike': -22,
     'chanceofrain': 0,
     'chanceofremdry': 0,
     'chanceofwindy': 0,
     'chanceofovercast': 0,
     'chanceofsunshine': 0,
     'chanceoffrost': 0,
     'chanceofhightemp': 0,
     'chanceoffog': 0,
     'chanceofsnow': 0,
     'chanceofthunder': 0,
     'uv_index': 1},
    {'time': '700',
     'temperature': -15,
     'wind_speed': 5,
     'wind_degree': 252,
     'wind_dir': 'WSW',
     'weather_code': 323,
     'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0011_light_snow_showers.png'],
     'weather_descriptions': ['Patchy light snow'],
     'precip': 0,
     'humidity': 96,
     'visibility': 10,
     'pressure': 1016,
     'cloudcover': 19,
     'heatindex': -15,
     'dewpoint': -16,
     'windchill': -19,
     'windgust': 10,
     'feelslike': -19,
     'chanceofrain': 0,
     'chanceofremdry': 0,
     'chanceofwindy': 0,
     'chanceofovercast': 0,
     'chanceofsunshine': 0,
     'chanceoffrost': 0,
     'chanceofhightemp': 0,
     'chanceoffog': 0,
     'chanceofsnow': 0,
     'chanceofthunder': 0,
     'uv_index': 1},
    {'time': '800',
     'temperature': -14,
     'wind_speed': 4,
     'wind_degree': 252,
     'wind_dir': 'WSW',
     'weather_code': 113,
     'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png'],
     'weather_descriptions': ['Sunny'],
     'precip': 0,
     'humidity': 95,
     'visibility': 10,
     'pressure': 1017,
     'cloudcover': 20,
     'heatindex': -14,
     'dewpoint': -15,
     'windchill': -16,
     'windgust': 7,
     'feelslike': -16,
     'chanceofrain': 0,
     'chanceofremdry': 0,
     'chanceofwindy': 0,
     'chanceofovercast': 0,
     'chanceofsunshine': 0,
     'chanceoffrost': 0,
     'chanceofhightemp': 0,
     'chanceoffog': 0,
     'chanceofsnow': 0,
     'chanceofthunder': 0,
     'uv_index': 2},
    {'time': '900',
     'temperature': -13,
     'wind_speed': 3,
     'wind_degree': 251,
     'wind_dir': 'WSW',
     'weather_code': 113,
     'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png'],
     'weather_descriptions': ['Sunny'],
     'precip': 0,
     'humidity': 94,
     'visibility': 10,
     'pressure': 1018,
     'cloudcover': 20,
     'heatindex': -13,
     'dewpoint': -14,
     'windchill': -13,
     'windgust': 4,
     'feelslike': -13,
     'chanceofrain': 0,
     'chanceofremdry': 0,
     'chanceofwindy': 0,
     'chanceofovercast': 0,
     'chanceofsunshine': 0,
     'chanceoffrost': 0,
     'chanceofhightemp': 0,
     'chanceoffog': 0,
     'chanceofsnow': 0,
     'chanceofthunder': 0,
     'uv_index': 2},
    {'time': '1000',
     'temperature': -12,
     'wind_speed': 4,
     'wind_degree': 236,
     'wind_dir': 'WSW',
     'weather_code': 113,
     'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png'],
     'weather_descriptions': ['Sunny'],
     'precip': 0,
     'humidity': 93,
     'visibility': 10,
     'pressure': 1018,
     'cloudcover': 25,
     'heatindex': -12,
     'dewpoint': -13,
     'windchill': -13,
     'windgust': 6,
     'feelslike': -13,
     'chanceofrain': 0,
     'chanceofremdry': 0,
     'chanceofwindy': 0,
     'chanceofovercast': 0,
     'chanceofsunshine': 0,
     'chanceoffrost': 0,
     'chanceofhightemp': 0,
     'chanceoffog': 0,
     'chanceofsnow': 0,
     'chanceofthunder': 0,
     'uv_index': 2},
    {'time': '1100',
     'temperature': -11,
     'wind_speed': 5,
     'wind_degree': 221,
     'wind_dir': 'SW',
     'weather_code': 116,
     'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png'],
     'weather_descriptions': ['Partly cloudy'],
     'precip': 0,
     'humidity': 93,
     'visibility': 10,
     'pressure': 1019,
     'cloudcover': 30,
     'heatindex': -11,
     'dewpoint': -12,
     'windchill': -14,
     'windgust': 7,
     'feelslike': -14,
     'chanceofrain': 0,
     'chanceofremdry': 0,
     'chanceofwindy': 0,
     'chanceofovercast': 0,
     'chanceofsunshine': 0,
     'chanceoffrost': 0,
     'chanceofhightemp': 0,
     'chanceoffog': 0,
     'chanceofsnow': 0,
     'chanceofthunder': 0,
     'uv_index': 2},
    {'time': '1200',
     'temperature': -10,
     'wind_speed': 6,
     'wind_degree': 206,
     'wind_dir': 'SSW',
     'weather_code': 116,
     'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png'],
     'weather_descriptions': ['Partly cloudy'],
     'precip': 0,
     'humidity': 93,
     'visibility': 10,
     'pressure': 1019,
     'cloudcover': 34,
     'heatindex': -10,
     'dewpoint': -11,
     'windchill': -14,
     'windgust': 9,
     'feelslike': -14,
     'chanceofrain': 0,
     'chanceofremdry': 0,
     'chanceofwindy': 0,
     'chanceofovercast': 0,
     'chanceofsunshine': 0,
     'chanceoffrost': 0,
     'chanceofhightemp': 0,
     'chanceoffog': 0,
     'chanceofsnow': 0,
     'chanceofthunder': 0,
     'uv_index': 2},
    {'time': '1300',
     'temperature': -11,
     'wind_speed': 7,
     'wind_degree': 209,
     'wind_dir': 'SSW',
     'weather_code': 116,
     'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png'],
     'weather_descriptions': ['Partly cloudy'],
     'precip': 0,
     'humidity': 95,
     'visibility': 7,
     'pressure': 1020,
     'cloudcover': 38,
     'heatindex': -11,
     'dewpoint': -12,
     'windchill': -15,
     'windgust': 12,
     'feelslike': -15,
     'chanceofrain': 0,
     'chanceofremdry': 0,
     'chanceofwindy': 0,
     'chanceofovercast': 0,
     'chanceofsunshine': 0,
     'chanceoffrost': 0,
     'chanceofhightemp': 0,
     'chanceoffog': 0,
     'chanceofsnow': 0,
     'chanceofthunder': 0,
     'uv_index': 2},
    {'time': '1400',
     'temperature': -12,
     'wind_speed': 7,
     'wind_degree': 212,
     'wind_dir': 'SSW',
     'weather_code': 260,
     'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0007_fog.png'],
     'weather_descriptions': ['Freezing fog'],
     'precip': 0,
     'humidity': 96,
     'visibility': 3,
     'pressure': 1020,
     'cloudcover': 41,
     'heatindex': -12,
     'dewpoint': -12,
     'windchill': -16,
     'windgust': 14,
     'feelslike': -16,
     'chanceofrain': 0,
     'chanceofremdry': 0,
     'chanceofwindy': 0,
     'chanceofovercast': 0,
     'chanceofsunshine': 0,
     'chanceoffrost': 0,
     'chanceofhightemp': 0,
     'chanceoffog': 0,
     'chanceofsnow': 0,
     'chanceofthunder': 0,
     'uv_index': 1},
    {'time': '1500',
     'temperature': -13,
     'wind_speed': 8,
     'wind_degree': 215,
     'wind_dir': 'SW',
     'weather_code': 260,
     'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0007_fog.png'],
     'weather_descriptions': ['Freezing fog'],
     'precip': 0,
     'humidity': 98,
     'visibility': 0,
     'pressure': 1021,
     'cloudcover': 45,
     'heatindex': -13,
     'dewpoint': -13,
     'windchill': -18,
     'windgust': 17,
     'feelslike': -18,
     'chanceofrain': 0,
     'chanceofremdry': 0,
     'chanceofwindy': 0,
     'chanceofovercast': 0,
     'chanceofsunshine': 0,
     'chanceoffrost': 0,
     'chanceofhightemp': 0,
     'chanceoffog': 0,
     'chanceofsnow': 0,
     'chanceofthunder': 0,
     'uv_index': 1},
    {'time': '1600',
     'temperature': -14,
     'wind_speed': 7,
     'wind_degree': 222,
     'wind_dir': 'SW',
     'weather_code': 260,
     'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0007_fog.png'],
     'weather_descriptions': ['Freezing fog'],
     'precip': 0,
     'humidity': 98,
     'visibility': 0,
     'pressure': 1022,
     'cloudcover': 36,
     'heatindex': -14,
     'dewpoint': -14,
     'windchill': -19,
     'windgust': 15,
     'feelslike': -19,
     'chanceofrain': 0,
     'chanceofremdry': 0,
     'chanceofwindy': 0,
     'chanceofovercast': 0,
     'chanceofsunshine': 0,
     'chanceoffrost': 0,
     'chanceofhightemp': 0,
     'chanceoffog': 0,
     'chanceofsnow': 0,
     'chanceofthunder': 0,
     'uv_index': 1},
    {'time': '1700',
     'temperature': -15,
     'wind_speed': 6,
     'wind_degree': 230,
     'wind_dir': 'SW',
     'weather_code': 260,
     'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0007_fog.png'],
     'weather_descriptions': ['Freezing fog'],
     'precip': 0,
     'humidity': 98,
     'visibility': 0,
     'pressure': 1022,
     'cloudcover': 27,
     'heatindex': -15,
     'dewpoint': -15,
     'windchill': -20,
     'windgust': 13,
     'feelslike': -20,
     'chanceofrain': 0,
     'chanceofremdry': 0,
     'chanceofwindy': 0,
     'chanceofovercast': 0,
     'chanceofsunshine': 0,
     'chanceoffrost': 0,
     'chanceofhightemp': 0,
     'chanceoffog': 0,
     'chanceofsnow': 0,
     'chanceofthunder': 0,
     'uv_index': 1},
    {'time': '1800',
     'temperature': -16,
     'wind_speed': 5,
     'wind_degree': 237,
     'wind_dir': 'WSW',
     'weather_code': 260,
     'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0007_fog.png'],
     'weather_descriptions': ['Freezing fog'],
     'precip': 0,
     'humidity': 98,
     'visibility': 0,
     'pressure': 1023,
     'cloudcover': 18,
     'heatindex': -16,
     'dewpoint': -17,
     'windchill': -22,
     'windgust': 11,
     'feelslike': -22,
     'chanceofrain': 0,
     'chanceofremdry': 0,
     'chanceofwindy': 0,
     'chanceofovercast': 0,
     'chanceofsunshine': 0,
     'chanceoffrost': 0,
     'chanceofhightemp': 0,
     'chanceoffog': 0,
     'chanceofsnow': 0,
     'chanceofthunder': 0,
     'uv_index': 1},
    {'time': '1900',
     'temperature': -17,
     'wind_speed': 5,
     'wind_degree': 241,
     'wind_dir': 'WSW',
     'weather_code': 260,
     'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0007_fog.png'],
     'weather_descriptions': ['Freezing fog'],
     'precip': 0,
     'humidity': 98,
     'visibility': 0,
     'pressure': 1023,
     'cloudcover': 31,
     'heatindex': -17,
     'dewpoint': -18,
     'windchill': -22,
     'windgust': 10,
     'feelslike': -22,
     'chanceofrain': 0,
     'chanceofremdry': 0,
     'chanceofwindy': 0,
     'chanceofovercast': 0,
     'chanceofsunshine': 0,
     'chanceoffrost': 0,
     'chanceofhightemp': 0,
     'chanceoffog': 0,
     'chanceofsnow': 0,
     'chanceofthunder': 0,
     'uv_index': 1},
    {'time': '2000',
     'temperature': -18,
     'wind_speed': 5,
     'wind_degree': 245,
     'wind_dir': 'WSW',
     'weather_code': 260,
     'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0007_fog.png'],
     'weather_descriptions': ['Freezing fog'],
     'precip': 0,
     'humidity': 99,
     'visibility': 0,
     'pressure': 1024,
     'cloudcover': 43,
     'heatindex': -18,
     'dewpoint': -19,
     'windchill': -23,
     'windgust': 10,
     'feelslike': -23,
     'chanceofrain': 0,
     'chanceofremdry': 0,
     'chanceofwindy': 0,
     'chanceofovercast': 0,
     'chanceofsunshine': 0,
     'chanceoffrost': 0,
     'chanceofhightemp': 0,
     'chanceoffog': 0,
     'chanceofsnow': 0,
     'chanceofthunder': 0,
     'uv_index': 1},
    {'time': '2100',
     'temperature': -20,
     'wind_speed': 4,
     'wind_degree': 248,
     'wind_dir': 'WSW',
     'weather_code': 260,
     'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0007_fog.png'],
     'weather_descriptions': ['Freezing fog'],
     'precip': 0,
     'humidity': 99,
     'visibility': 0,
     'pressure': 1024,
     'cloudcover': 56,
     'heatindex': -20,
     'dewpoint': -20,
     'windchill': -23,
     'windgust': 9,
     'feelslike': -23,
     'chanceofrain': 0,
     'chanceofremdry': 0,
     'chanceofwindy': 0,
     'chanceofovercast': 0,
     'chanceofsunshine': 0,
     'chanceoffrost': 0,
     'chanceofhightemp': 0,
     'chanceoffog': 0,
     'chanceofsnow': 0,
     'chanceofthunder': 0,
     'uv_index': 1},
    {'time': '2200',
     'temperature': -20,
     'wind_speed': 4,
     'wind_degree': 215,
     'wind_dir': 'SW',
     'weather_code': 260,
     'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0007_fog.png'],
     'weather_descriptions': ['Freezing fog'],
     'precip': 0,
     'humidity': 99,
     'visibility': 0,
     'pressure': 1025,
     'cloudcover': 69,
     'heatindex': -20,
     'dewpoint': -20,
     'windchill': -23,
     'windgust': 8,
     'feelslike': -23,
     'chanceofrain': 0,
     'chanceofremdry': 0,
     'chanceofwindy': 0,
     'chanceofovercast': 0,
     'chanceofsunshine': 0,
     'chanceoffrost': 0,
     'chanceofhightemp': 0,
     'chanceoffog': 0,
     'chanceofsnow': 0,
     'chanceofthunder': 0,
     'uv_index': 1},
    {'time': '2300',
     'temperature': -20,
     'wind_speed': 3,
     'wind_degree': 181,
     'wind_dir': 'S',
     'weather_code': 260,
     'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0007_fog.png'],
     'weather_descriptions': ['Freezing fog'],
     'precip': 0,
     'humidity': 99,
     'visibility': 0,
     'pressure': 1025,
     'cloudcover': 82,
     'heatindex': -20,
     'dewpoint': -20,
     'windchill': -24,
     'windgust': 6,
     'feelslike': -24,
     'chanceofrain': 0,
     'chanceofremdry': 0,
     'chanceofwindy': 0,
     'chanceofovercast': 0,
     'chanceofsunshine': 0,
     'chanceoffrost': 0,
     'chanceofhightemp': 0,
     'chanceoffog': 0,
     'chanceofsnow': 0,
     'chanceofthunder': 0,
     'uv_index': 1}]},
  '2017-01-07': {'date': '2017-01-07',
   'date_epoch': 1483747200,
   'astro': {'sunrise': '08:35 AM',
    'sunset': '05:26 PM',
    'moonrise': '01:45 PM',
    'moonset': '02:45 AM',
    'moon_phase': 'First Quarter',
    'moon_illumination': 63},
   'mintemp': -21,
   'maxtemp': -11,
   'avgtemp': -15,
   'totalsnow': 0.1,
   'sunhour': 8.7,
   'uv_index': 1,
   'hourly': [{'time': '0',
     'temperature': -21,
     'wind_speed': 2,
     'wind_degree': 147,
     'wind_dir': 'SSE',
     'weather_code': 260,
     'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0007_fog.png'],
     'weather_descriptions': ['Freezing fog'],
     'precip': 0,
     'humidity': 99,
     'visibility': 0,
     'pressure': 1026,
     'cloudcover': 96,
     'heatindex': -21,
     'dewpoint': -21,
     'windchill': -24,
     'windgust': 5,
     'feelslike': -24,
     'chanceofrain': 0,
     'chanceofremdry': 0,
     'chanceofwindy': 0,
     'chanceofovercast': 0,
     'chanceofsunshine': 0,
     'chanceoffrost': 0,
     'chanceofhightemp': 0,
     'chanceoffog': 0,
     'chanceofsnow': 0,
     'chanceofthunder': 0,
     'uv_index': 1},
    {'time': '100',
     'temperature': -18,
     'wind_speed': 3,
     'wind_degree': 164,
     'wind_dir': 'SSE',
     'weather_code': 260,
     'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0007_fog.png'],
     'weather_descriptions': ['Freezing fog'],
     'precip': 0,
     'humidity': 99,
     'visibility': 2,
     'pressure': 1026,
     'cloudcover': 96,
     'heatindex': -18,
     'dewpoint': -19,
     'windchill': -21,
     'windgust': 6,
     'feelslike': -21,
     'chanceofrain': 0,
     'chanceofremdry': 0,
     'chanceofwindy': 0,
     'chanceofovercast': 0,
     'chanceofsunshine': 0,
     'chanceoffrost': 0,
     'chanceofhightemp': 0,
     'chanceoffog': 0,
     'chanceofsnow': 0,
     'chanceofthunder': 0,
     'uv_index': 1},
    {'time': '200',
     'temperature': -16,
     'wind_speed': 3,
     'wind_degree': 181,
     'wind_dir': 'S',
     'weather_code': 332,
     'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0036_cloudy_with_heavy_snow_night.png'],
     'weather_descriptions': ['Moderate snow'],
     'precip': 0,
     'humidity': 98,
     'visibility': 3,
     'pressure': 1027,
     'cloudcover': 97,
     'heatindex': -16,
     'dewpoint': -17,
     'windchill': -19,
     'windgust': 7,
     'feelslike': -19,
     'chanceofrain': 0,
     'chanceofremdry': 0,
     'chanceofwindy': 0,
     'chanceofovercast': 0,
     'chanceofsunshine': 0,
     'chanceoffrost': 0,
     'chanceofhightemp': 0,
     'chanceoffog': 0,
     'chanceofsnow': 0,
     'chanceofthunder': 0,
     'uv_index': 1},
    {'time': '300',
     'temperature': -14,
     'wind_speed': 4,
     'wind_degree': 197,
     'wind_dir': 'SSW',
     'weather_code': 332,
     'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0036_cloudy_with_heavy_snow_night.png'],
     'weather_descriptions': ['Moderate snow'],
     'precip': 0,
     'humidity': 98,
     'visibility': 5,
     'pressure': 1027,
     'cloudcover': 98,
     'heatindex': -14,
     'dewpoint': -14,
     'windchill': -17,
     'windgust': 8,
     'feelslike': -17,
     'chanceofrain': 0,
     'chanceofremdry': 0,
     'chanceofwindy': 0,
     'chanceofovercast': 0,
     'chanceofsunshine': 0,
     'chanceoffrost': 0,
     'chanceofhightemp': 0,
     'chanceoffog': 0,
     'chanceofsnow': 0,
     'chanceofthunder': 0,
     'uv_index': 1},
    {'time': '400',
     'temperature': -16,
     'wind_speed': 3,
     'wind_degree': 156,
     'wind_dir': 'SSE',
     'weather_code': 332,
     'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0036_cloudy_with_heavy_snow_night.png'],
     'weather_descriptions': ['Moderate snow'],
     'precip': 0,
     'humidity': 98,
     'visibility': 3,
     'pressure': 1028,
     'cloudcover': 82,
     'heatindex': -16,
     'dewpoint': -16,
     'windchill': -18,
     'windgust': 6,
     'feelslike': -18,
     'chanceofrain': 0,
     'chanceofremdry': 0,
     'chanceofwindy': 0,
     'chanceofovercast': 0,
     'chanceofsunshine': 0,
     'chanceoffrost': 0,
     'chanceofhightemp': 0,
     'chanceoffog': 0,
     'chanceofsnow': 0,
     'chanceofthunder': 0,
     'uv_index': 1},
    {'time': '500',
     'temperature': -18,
     'wind_speed': 2,
     'wind_degree': 116,
     'wind_dir': 'ESE',
     'weather_code': 260,
     'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0007_fog.png'],
     'weather_descriptions': ['Freezing fog'],
     'precip': 0,
     'humidity': 99,
     'visibility': 2,
     'pressure': 1028,
     'cloudcover': 67,
     'heatindex': -18,
     'dewpoint': -18,
     'windchill': -19,
     'windgust': 5,
     'feelslike': -19,
     'chanceofrain': 0,
     'chanceofremdry': 0,
     'chanceofwindy': 0,
     'chanceofovercast': 0,
     'chanceofsunshine': 0,
     'chanceoffrost': 0,
     'chanceofhightemp': 0,
     'chanceoffog': 0,
     'chanceofsnow': 0,
     'chanceofthunder': 0,
     'uv_index': 1},
    {'time': '600',
     'temperature': -20,
     'wind_speed': 1,
     'wind_degree': 75,
     'wind_dir': 'ENE',
     'weather_code': 260,
     'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0007_fog.png'],
     'weather_descriptions': ['Freezing fog'],
     'precip': 0,
     'humidity': 100,
     'visibility': 0,
     'pressure': 1029,
     'cloudcover': 51,
     'heatindex': -20,
     'dewpoint': -20,
     'windchill': -20,
     'windgust': 3,
     'feelslike': -20,
     'chanceofrain': 0,
     'chanceofremdry': 0,
     'chanceofwindy': 0,
     'chanceofovercast': 0,
     'chanceofsunshine': 0,
     'chanceoffrost': 0,
     'chanceofhightemp': 0,
     'chanceoffog': 0,
     'chanceofsnow': 0,
     'chanceofthunder': 0,
     'uv_index': 1},
    {'time': '700',
     'temperature': -18,
     'wind_speed': 2,
     'wind_degree': 146,
     'wind_dir': 'SSE',
     'weather_code': 260,
     'weather_icons': ['https://assets.weatherstack.com/images/wsymbols01_png_64/wsymbol_0007_fog.png'],
     'weather_descriptions': ['Freezing fog'],
     'precip': 0,
     'humidity': 98,
     'visibility': 3,
     'pressure': 1029,
     'cloudcover': 51,
     'heatindex': -18,
     'dewpoint': -18,
     'windchill': -18,
     'windgust': 3,
     'feelslike': -18,
     'chanceofrain': 0,
     'chanceofremdry': 0,
     'chanceofwindy': 0,
     'chanceofovercast': 0,
     'chanceofsunshine': 0,
     'chanceoffrost': 0,
     'chanceofhightemp': 0,
     'chanceoffog': 0,
     'chanceofsnow': 0,
     'chanceofthunder': 0,
     'uv_index': 1},
    {'time': '800', etc....

但是,我需要一个只有 3 列 date, time,precip 的数据框,不包括索引。

如何根据响应创建数据框,使其看起来像这样:

print(df)
             date         time  precip
0      2017-01-06     00:00:00     0.1
1      2017-01-06     01:00:00     0.0
2      2017-01-06     02:00:00     0.0
3      2017-01-06     03:00:00     0.0
4      2017-01-06     04:00:00     0.0
...     ...                 ...     ...

1 个答案:

答案 0 :(得分:1)

这行得通吗:

data = api_response1['historical']
df = pd.DataFrame.from_records([
         {
             'date': date,
             'time': f"{int(observation['time'])//100:0>2}:00:00",
             'precip': float(observation['precip'])
         }
         for date in data
         for observation in data[date]['hourly']
     ])

我的结果 (print(df)):

          date      time  precip
0   2017-01-06  00:00:00     0.1
1   2017-01-06  01:00:00     0.0
2   2017-01-06  02:00:00     0.0
... ...         ...          ...
29  2017-01-07  05:00:00     0.0
30  2017-01-07  06:00:00     0.0
31  2017-01-07  07:00:00     0.0