I have a response from an api that contains a data sump from a heating system, structured as dictionaries with nested dictionaries that contain lists that contain dictionaries.
eg
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}]
}
}}
I want to extract the ['insideTemperature']['datapoints'] timestamp and celsius values from the above (actual data spans more periods) and place them as columns in a new pd.DataFrame along with other data from the 'humidity' key. In due course, I want to merge this with data from a separate API call that has a similar structure, though may not have consistent timestamp values.
A number of the top level dictionaries contain summary data (eg min and max values) so can be ignored. Equally, conversion from celsius to f etc, is easy to do if needed, so I don't want to pull this data.
What is the best way to cleanly create a DataFile that lists the timestamp, temperature in Celsius and humidity from this query that I can then join with other query outputs?
So far, I have been using the following:
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)
Being new to coding in python, I am wondering if there is a far quicker way of extracting the data from the original downloaded data, straight into a clean Pandas DataFrame?? Grateful for any suggestions.
You can use json_normalize
to get the data:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.