您好,我正在尝试从Mars Insight API显示火星天气。发生的问题是数据以JSON格式返回并且具有三个级别API Documentation。我可以用3-4种不同的方法来拉主键,但是当我尝试获得次要或三次键时,也就可以了。
import requests
import json
import pandas as pd
from pandas.io.json import json_normalize
API_url = "https://api.nasa.gov/insight_weather/?api_key=nTal99zKlhGbl0N8F0V9iUofifMdcwyOHw64CrVm&feedtype=json&ver=1.0"
API_data = requests.get(API_url).json()
# define weather data attributes
#AT = {'AT':API_data['sol_keys'[1,2,3]]}
#PRE = {'PRE':API_data['sol_keys']}
#HWS = {'HWS':API_data['sol_keys']}
#Season= {'Season':API_data['sol_keys']}
#WD = {'WD':API_data['sol_keys']}
#most_common = {'most_common':API_data['sol_keys']}
context = {'sol_keys': API_data["sol_keys"]}
data =json_normalize(API_data, 'sol_keys', '301','AT')
print (data)
dicts
flatten_json
功能,将用于展平data
def flatten_json(nested_json: dict, exclude: list=['']) -> dict:
"""
Flatten a list of nested dicts.
"""
out = dict()
def flatten(x: (list, dict, str), name: str='', exclude=exclude):
if type(x) is dict:
for a in x:
if a not in exclude:
flatten(x[a], f'{name}{a}_')
elif type(x) is list:
i = 0
for a in x:
flatten(a, f'{name}{i}_')
i += 1
else:
out[name[:-1]] = x
flatten(nested_json)
return out
import pandas as pd
from pandas.io.json import json_normalize
import requests
API_url = "https://api.nasa.gov/insight_weather/?api_key=nTal99zKlhGbl0N8F0V9iUofifMdcwyOHw64CrVm&feedtype=json&ver=1.0"
API_data = requests.get(API_url).json()
data =json_normalize(API_data, 'sol_keys', '301', 'AT')
df = pd.DataFrame([flatten_json(x) for x in data['AT301']])
AT_av AT_ct AT_mn AT_mx First_UTC HWS_av HWS_ct HWS_mn HWS_mx Last_UTC PRE_av PRE_ct PRE_mn PRE_mx Season WD_1_compass_degrees WD_1_compass_point WD_1_compass_right WD_1_compass_up WD_1_ct WD_10_compass_degrees WD_10_compass_point WD_10_compass_right WD_10_compass_up WD_10_ct WD_11_compass_degrees WD_11_compass_point WD_11_compass_right WD_11_compass_up WD_11_ct WD_12_compass_degrees WD_12_compass_point WD_12_compass_right WD_12_compass_up WD_12_ct WD_13_compass_degrees WD_13_compass_point WD_13_compass_right WD_13_compass_up WD_13_ct WD_2_compass_degrees WD_2_compass_point WD_2_compass_right WD_2_compass_up WD_2_ct WD_3_compass_degrees WD_3_compass_point WD_3_compass_right WD_3_compass_up WD_3_ct WD_5_compass_degrees WD_5_compass_point WD_5_compass_right WD_5_compass_up WD_5_ct WD_6_compass_degrees WD_6_compass_point WD_6_compass_right WD_6_compass_up WD_6_ct WD_7_compass_degrees WD_7_compass_point WD_7_compass_right WD_7_compass_up WD_7_ct WD_8_compass_degrees WD_8_compass_point WD_8_compass_right WD_8_compass_up WD_8_ct WD_9_compass_degrees WD_9_compass_point WD_9_compass_right WD_9_compass_up WD_9_ct WD_most_common_compass_degrees WD_most_common_compass_point WD_most_common_compass_right WD_most_common_compass_up WD_most_common_ct
-69.684 342720 -103.886 -26.371 2019-10-01T11:46:39Z 4.63 158626 0.129 17.919 2019-10-02T12:26:13Z 727.941 153492 711.7187 743.1005 spring 22.5 NNE 0.382683 0.92388 4 225.0 SW -0.707107 -0.707107 26723 247.5 WSW -0.92388 -0.382683 15528 270.0 W -1.0 -0.0 3136 292.5 WNW -0.92388 0.382683 2 45.0 NE 0.707107 0.707107 6 67.5 ENE 0.92388 0.382683 688 112.5 ESE 0.92388 -0.382683 3387 135.0 SE 0.707107 -0.707107 40327 157.5 SSE 0.382683 -0.92388 31608 180.0 S 0.0 -1.0 8520 202.5 SSW -0.382683 -0.92388 28697 135.0 SE 0.707107 -0.707107 40327
-69.684 342720 -103.886 -26.371 2019-10-01T11:46:39Z 4.63 158626 0.129 17.919 2019-10-02T12:26:13Z 727.941 153492 711.7187 743.1005 spring 22.5 NNE 0.382683 0.92388 4 225.0 SW -0.707107 -0.707107 26723 247.5 WSW -0.92388 -0.382683 15528 270.0 W -1.0 -0.0 3136 292.5 WNW -0.92388 0.382683 2 45.0 NE 0.707107 0.707107 6 67.5 ENE 0.92388 0.382683 688 112.5 ESE 0.92388 -0.382683 3387 135.0 SE 0.707107 -0.707107 40327 157.5 SSE 0.382683 -0.92388 31608 180.0 S 0.0 -1.0 8520 202.5 SSW -0.382683 -0.92388 28697 135.0 SE 0.707107 -0.707107 40327
-69.684 342720 -103.886 -26.371 2019-10-01T11:46:39Z 4.63 158626 0.129 17.919 2019-10-02T12:26:13Z 727.941 153492 711.7187 743.1005 spring 22.5 NNE 0.382683 0.92388 4 225.0 SW -0.707107 -0.707107 26723 247.5 WSW -0.92388 -0.382683 15528 270.0 W -1.0 -0.0 3136 292.5 WNW -0.92388 0.382683 2 45.0 NE 0.707107 0.707107 6 67.5 ENE 0.92388 0.382683 688 112.5 ESE 0.92388 -0.382683 3387 135.0 SE 0.707107 -0.707107 40327 157.5 SSE 0.382683 -0.92388 31608 180.0 S 0.0 -1.0 8520 202.5 SSW -0.382683 -0.92388 28697 135.0 SE 0.707107 -0.707107 40327
-69.684 342720 -103.886 -26.371 2019-10-01T11:46:39Z 4.63 158626 0.129 17.919 2019-10-02T12:26:13Z 727.941 153492 711.7187 743.1005 spring 22.5 NNE 0.382683 0.92388 4 225.0 SW -0.707107 -0.707107 26723 247.5 WSW -0.92388 -0.382683 15528 270.0 W -1.0 -0.0 3136 292.5 WNW -0.92388 0.382683 2 45.0 NE 0.707107 0.707107 6 67.5 ENE 0.92388 0.382683 688 112.5 ESE 0.92388 -0.382683 3387 135.0 SE 0.707107 -0.707107 40327 157.5 SSE 0.382683 -0.92388 31608 180.0 S 0.0 -1.0 8520 202.5 SSW -0.382683 -0.92388 28697 135.0 SE 0.707107 -0.707107 40327
-69.684 342720 -103.886 -26.371 2019-10-01T11:46:39Z 4.63 158626 0.129 17.919 2019-10-02T12:26:13Z 727.941 153492 711.7187 743.1005 spring 22.5 NNE 0.382683 0.92388 4 225.0 SW -0.707107 -0.707107 26723 247.5 WSW -0.92388 -0.382683 15528 270.0 W -1.0 -0.0 3136 292.5 WNW -0.92388 0.382683 2 45.0 NE 0.707107 0.707107 6 67.5 ENE 0.92388 0.382683 688 112.5 ESE 0.92388 -0.382683 3387 135.0 SE 0.707107 -0.707107 40327 157.5 SSE 0.382683 -0.92388 31608 180.0 S 0.0 -1.0 8520 202.5 SSW -0.382683 -0.92388 28697 135.0 SE 0.707107 -0.707107 40327
-69.684 342720 -103.886 -26.371 2019-10-01T11:46:39Z 4.63 158626 0.129 17.919 2019-10-02T12:26:13Z 727.941 153492 711.7187 743.1005 spring 22.5 NNE 0.382683 0.92388 4 225.0 SW -0.707107 -0.707107 26723 247.5 WSW -0.92388 -0.382683 15528 270.0 W -1.0 -0.0 3136 292.5 WNW -0.92388 0.382683 2 45.0 NE 0.707107 0.707107 6 67.5 ENE 0.92388 0.382683 688 112.5 ESE 0.92388 -0.382683 3387 135.0 SE 0.707107 -0.707107 40327 157.5 SSE 0.382683 -0.92388 31608 180.0 S 0.0 -1.0 8520 202.5 SSW -0.382683 -0.92388 28697 135.0 SE 0.707107 -0.707107 40327
-69.684 342720 -103.886 -26.371 2019-10-01T11:46:39Z 4.63 158626 0.129 17.919 2019-10-02T12:26:13Z 727.941 153492 711.7187 743.1005 spring 22.5 NNE 0.382683 0.92388 4 225.0 SW -0.707107 -0.707107 26723 247.5 WSW -0.92388 -0.382683 15528 270.0 W -1.0 -0.0 3136 292.5 WNW -0.92388 0.382683 2 45.0 NE 0.707107 0.707107 6 67.5 ENE 0.92388 0.382683 688 112.5 ESE 0.92388 -0.382683 3387 135.0 SE 0.707107 -0.707107 40327 157.5 SSE 0.382683 -0.92388 31608 180.0 S 0.0 -1.0 8520 202.5 SSW -0.382683 -0.92388 28697 135.0 SE 0.707107 -0.707107 40327