如何在Python中从API结果创建数据框

时间:2019-11-26 05:34:46

标签: python pandas dataframe python-requests

我从Python发起的Request.get呼叫给了我类似下面的输出

response.text

'{"results":[{"place":{"type":"coord","value":"44.164:28.641","lat":44.164,"lon":28.641,"tz":"Europe/Bucharest"},"measures":[{"ts":1575331200000,"date":"2019-12-03","temperature_2m":11.78,"temperature_2m_min":11.75,"temperature_2m_max":12.46,"windspeed":3.25,"direction":"SSW","wind_gust":5.43,"relative_humidity_2m":88,"sea_level_pressure":1014,"sky_cover":"cloudy","precipitation":0.0,"snow_depth":0,"thunderstorm":"N","fog":"M"}]},{"place":{"type":"coord","value":"53.546:9.98","lat":53.546,"lon":9.98,"tz":"Europe/Berlin"},"measures":[{"ts":1575331200000,"date":"2019-12-03","temperature_2m":-0.55,"temperature_2m_min":-0.8,"temperature_2m_max":-0.35,"windspeed":3.65,"direction":"WSW","wind_gust":8.62,"relative_humidity_2m":88,"sea_level_pressure":1025,"sky_cover":"mostly_clear","precipitation":0.0,"snow_depth":0,"thunderstorm":"N","fog":"M"}]}]}'

我要为此制作数据框dp

coord   date    direction   fog precipitation   relative_humidity_2m    sea_level_pressure  sky_cover   snow_depth  temperature_2m  temperature_2m_max  temperature_2m_min  thunderstorm    ts  wind_gust   windspeed
44.164:28.641   3/12/2019   SSW M                0                            88            1014        cloudy          0             11.78           12.46                  11.75              N      1.57533E+12     5.43           3.25
53.546:9.98 3/12/2019       WSW M                0                            88            1025        mostly_clear    0             -0.55           -0.35                     -0.8                    N      1.57533E+12    8.62            3.65

我也有两种情况:

response.status == 200,API在response.text中给出值,然后我要存储此数据帧 response.status!=200。API没有给出值response.text,那么我想将除坐标以外的所有其他字段填充为空

这怎么办?

正常数据给出以下指示:

{'results': [{'place': {'type': 'coord',
    'value': '44.164:28.641',
    'lat': 44.164,
    'lon': 28.641,
    'tz': 'Europe/Bucharest'},
'measures': [{'ts': 1575331200000,
    'date': '2019-12-03',
    'temperature_2m': 11.78,
    'temperature_2m_min': 11.75,
    'temperature_2m_max': 12.46,
    'windspeed': 3.25,
    'direction': 'SSW',
    'wind_gust': 5.43,
    'relative_humidity_2m': 88,
    'sea_level_pressure': 1014,
    'sky_cover': 'cloudy',
    'precipitation': 0.0,
    'snow_depth': 0,
    'thunderstorm': 'N',
    'fog': 'M'}]},
{'place': {'type': 'coord',
    'value': '53.546:9.98',
    'lat': 53.546,
    'lon': 9.98,
    'tz': 'Europe/Berlin'},
'measures': [{'ts': 1575331200000,
    'date': '2019-12-03',
    'temperature_2m': -0.55,
    'temperature_2m_min': -0.8,
    'temperature_2m_max': -0.35,
    'windspeed': 3.65,
    'direction': 'WSW',
    'wind_gust': 8.62,
    'relative_humidity_2m': 88,
    'sea_level_pressure': 1025,
    'sky_cover': 'mostly_clear',
    'precipitation': 0.0,
    'snow_depth': 0,
    'thunderstorm': 'N',
    'fog': 'M'}]}]}

3 个答案:

答案 0 :(得分:5)

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

response_data = json.loads(responese.text)
df = json_normalize(response_data['results'])

df1 = json_normalize([df["measures"][i][0] for i in range(0, df.shape[0])])

final_df = pd.concat([df, df1], axis=1)

final_df.drop("measures", axis=1, inplace=True)

答案 1 :(得分:4)

您可以尝试DataFrame.from_records。只要“位置”和“度量”中没有公用键,该解决方案就可以使用。

import json

d = json.loads(responese.text)

def update(a, b):
    c = dict()
    c.update(a)
    c.update(b)
    return c


pd.DataFrame.from_records(
    (
        update(result['place'], measure)
        for result in d['results']
        for measure in result['measures']
    )
)

结果:

    type    value   lat lon tz  ts  date    temperature_2m  temperature_2m_min  temperature_2m_max  windspeed   direction   wind_gust   relative_humidity_2m    sea_level_pressure  sky_cover   precipitation   snow_depth  thunderstorm    fog
0   coord   44.164:28.641   44.164  28.641  Europe/Bucharest    1575331200000   2019-12-03  11.78   11.75   12.46   3.25    SSW 5.43    88  1014    cloudy  0.0 0   N   M
1   coord   53.546:9.98 53.546  9.980   Europe/Berlin   1575331200000   2019-12-03  -0.55   -0.80   -0.35   3.65    WSW 8.62    88  1025    mostly_clear    0.0 0   N   M

答案 2 :(得分:2)

d = eval(response.text)

columns_needed = ['coord', 'date', 'direction', 'fog', 'precipitation',
               'relative_humidity_2m', 'sea_level_pressure', 'sky_cover', 'snow_depth',
               'temperature_2m', 'temperature_2m_max', 'temperature_2m_min',
           'thunderstorm', 'ts', 'wind_gust', 'windspeed']

li = []
for i in d.get('results'):
    temp_d = dict()
    place = i.get('place')
    measure = i.get('measures')[0]
    temp_d[place.get('type')] = place.get('value')
    temp_d.update(measure)
    li.append(temp_d)
final_df = pd.DataFrame(li)
final_df = final_df.loc[:,columns_needed]

print(final_df)