我收到一个api的响应,该api包含供暖系统的数据集,结构为带有嵌套字典的字典,嵌套字典包含包含字典的列表。
例如
sample = {"zoneType": "HEATING",
"interval": {"from": "2020-10-23T22:45:00.000Z", "to": "2020-10-24T23:15:00.000Z"},
"hoursInDay": 24,
"measuredData": {
"measuringDeviceConnected": {
"timeSeriesType": "dataIntervals",
"valueType": "boolean",
"dataIntervals": [{
"from": "2020-10-23T22:45:00.000Z", "to": "2020-10-24T23:15:00.000Z", "value": True}]
},
"insideTemperature": {
"timeSeriesType": "dataPoints",
"valueType": "temperature",
"min": {
"celsius": 19.34,
"fahrenheit": 66.81},
"max": {
"celsius": 20.6,
"fahrenheit": 69.08},
"dataPoints": [
{"timestamp": "2020-10-23T22:45:00.000Z", "value": {"celsius": 20.6, "fahrenheit": 69.08}},
{"timestamp": "2020-10-23T23:00:00.000Z", "value": {"celsius": 20.55, "fahrenheit": 68.99}},
{"timestamp": "2020-10-23T23:15:00.000Z", "value": {"celsius": 20.53, "fahrenheit": 68.95}},
{"timestamp": "2020-10-23T23:30:00.000Z", "value": {"celsius": 20.51, "fahrenheit": 68.92}},
{"timestamp": "2020-10-23T23:45:00.000Z", "value": {"celsius": 20.48, "fahrenheit": 68.86}},
{"timestamp": "2020-10-24T00:00:00.000Z", "value": {"celsius": 20.48, "fahrenheit": 68.86}},
{"timestamp": "2020-10-24T00:15:00.000Z", "value": {"celsius": 20.44, "fahrenheit": 68.79}}]
},
"humidity": {
"timeSeriesType": "dataPoints",
"valueType": "percentage",
"percentageUnit": "UNIT_INTERVAL",
"min": 0.615,
"max": 0.627,
"dataPoints": [
{"timestamp": "2020-10-23T22:45:00.000Z", "value": 0.615},
{"timestamp": "2020-10-23T23:00:00.000Z", "value": 0.615},
{"timestamp": "2020-10-23T23:15:00.000Z", "value": 0.619},
{"timestamp": "2020-10-23T23:30:00.000Z", "value": 0.620},
{"timestamp": "2020-10-23T23:45:00.000Z", "value": 0.621},
{"timestamp": "2020-10-24T00:00:00.000Z", "value": 0.623},
{"timestamp": "2020-10-24T00:15:00.000Z", "value": 0.627}]
}
}}
我想从上面提取['insideTemperature'] ['datapoints']时间戳和摄氏值(实际数据跨越更多时间段),并将它们与''中的其他数据一起作为列放置在新的pd.DataFrame中。湿度”键。在适当的时候,我想将其与结构相似的单独API调用中的数据合并,尽管可能没有一致的时间戳值。
许多顶级词典都包含摘要数据(例如,最小值和最大值),因此可以忽略。同样,从摄氏到f等的转换很容易,如果需要的话,所以我不想提取这些数据。
干净地创建一个数据文件以列出该查询的时间戳,摄氏温度和湿度的最佳方法是什么,然后我可以将其与其他查询输出结合起来?
到目前为止,我一直在使用以下内容:
import pandas as pd
df = pd.DataFrame(sample['measuredData']['insideTemperature']['dataPoints'])
## remove column that contains dictionary data, leaving time data
df.drop(labels='value', axis=1, inplace=True)
## get temp data into new column
input_data_point = sample['measuredData']['insideTemperature']['dataPoints']
temps = []
for i in input_data_point:
temps.append(i['value']['celsius'])
df['inside_temp_c'] = pd.DataFrame(temps)
## repeat for humidity
input_data_point = sample['measuredData']['humidity']['dataPoints']
temps = []
for i in input_data_point:
temps.append(i['value'])
df['humidity_pct'] = pd.DataFrame(temps)
我是Python的新手,我想知道是否有一种更快的方法来从原始下载的数据中提取数据,直接提取到一个干净的Pandas DataFrame中?感谢任何建议。
答案 0 :(得分:0)
您可以使用json_normalize
来获取数据:
df1 = pd.json_normalize(sample,
record_path=['measuredData', 'insideTemperature', 'dataPoints'],
meta=['zoneType'])
print(df1)
df2 = pd.json_normalize(sample,
record_path=['measuredData', 'humidity', 'dataPoints'],
meta=['zoneType'])
print(df2)
df1:
timestamp value.celsius value.fahrenheit zoneType
0 2020-10-23T22:45:00.000Z 20.60 69.08 HEATING
1 2020-10-23T23:00:00.000Z 20.55 68.99 HEATING
2 2020-10-23T23:15:00.000Z 20.53 68.95 HEATING
3 2020-10-23T23:30:00.000Z 20.51 68.92 HEATING
4 2020-10-23T23:45:00.000Z 20.48 68.86 HEATING
5 2020-10-24T00:00:00.000Z 20.48 68.86 HEATING
6 2020-10-24T00:15:00.000Z 20.44 68.79 HEATING
df2:
timestamp value zoneType
0 2020-10-23T22:45:00.000Z 0.615 HEATING
1 2020-10-23T23:00:00.000Z 0.615 HEATING
2 2020-10-23T23:15:00.000Z 0.619 HEATING