{ "type": "NORMAL", "var": { "id": 2808147, "name": "A相电压", "unit": "kV", "type": "Analog", "sn": "Ahhy_CPZX_G101__YC000", "device_sn": "Ahhy_CPZX_G101", "device_name": "进线", "parent_index": "Ahhy", "zero_meaning": null, "one_meaning": null, "var_code": "Ua", "record_period": "5分钟", "rw": 1 }, "name": "A相电压", "unit": "kV", "time_keys": [ "2023-05-25 00:00:00.000", "2023-05-25 00:05:00.000", "2023-05-25 00:10:00.000", "2023-05-25 00:15:00.000", "2023-05-25 00:20:00.000" ], "datas": [ 6.079, 6.079, 6.079, 6.079, 6.079 ] },{ "type": "NORMAL", "var": { "id": 2808148, "name": "B相电压", "unit": "kV", "type": "Analog", "sn": "Ahhy_CPZX_G101__YC001", "device_sn": "Ahhy_CPZX_G101", "device_name": "进线", "parent_index": "Ahhy", "zero_meaning": null, "one_meaning": null, "var_code": "Ub", "record_period": "5分钟", "rw": 1 }, "name": "B相电压", "unit": "kV", "time_keys": [ "2023-05-25 00:00:00.000", "2023-05-25 00:05:00.000", "2023-05-25 00:10:00.000", "2023-05-25 00:15:00.000", "2023-05-25 00:20:00.000" ], "datas": [ 6.079, 6.079, 6.064, 6.074, 6.065 ] } 以上为返回的数据格式,如何处理成 ,时间 A相电压 B相电压这种表格形式
时间: 2023-06-28 13:13:53 浏览: 155
matplotlib-3.6.3-cp39-cp39-linux_armv7l.whl
您可以使用Python中的pandas库来处理这个数据格式并转换为表格形式。具体步骤如下:
1. 将返回的数据格式转换为字典类型,命名为data_dict。
2. 使用pandas库中的DataFrame函数将数据转换为DataFrame格式。
3. 使用DataFrame中的pivot函数将数据根据时间键(time_keys)进行透视,转换为表格形式。
示例代码如下:
```
import pandas as pd
# 将返回的数据格式转换为字典类型
data_dict = {
"data": [
{
"type": "NORMAL",
"var": {
"id": 2808147,
"name": "A相电压",
"unit": "kV",
"type": "Analog",
"sn": "Ahhy_CPZX_G101__YC000",
"device_sn": "Ahhy_CPZX_G101",
"device_name": "进线",
"parent_index": "Ahhy",
"zero_meaning": None,
"one_meaning": None,
"var_code": "Ua",
"record_period": "5分钟",
"rw": 1
},
"name": "A相电压",
"unit": "kV",
"time_keys": [
"2023-05-25 00:00:00.000",
"2023-05-25 00:05:00.000",
"2023-05-25 00:10:00.000",
"2023-05-25 00:15:00.000",
"2023-05-25 00:20:00.000"
],
"datas": [
6.079,
6.079,
6.079,
6.079,
6.079
]
},
{
"type": "NORMAL",
"var": {
"id": 2808148,
"name": "B相电压",
"unit": "kV",
"type": "Analog",
"sn": "Ahhy_CPZX_G101__YC001",
"device_sn": "Ahhy_CPZX_G101",
"device_name": "进线",
"parent_index": "Ahhy",
"zero_meaning": None,
"one_meaning": None,
"var_code": "Ub",
"record_period": "5分钟",
"rw": 1
},
"name": "B相电压",
"unit": "kV",
"time_keys": [
"2023-05-25 00:00:00.000",
"2023-05-25 00:05:00.000",
"2023-05-25 00:10:00.000",
"2023-05-25 00:15:00.000",
"2023-05-25 00:20:00.000"
],
"datas": [
6.079,
6.079,
6.064,
6.074,
6.065
]
}
]
}
# 将数据转换为DataFrame格式
df = pd.DataFrame(data_dict['data'])
# 将时间键设为索引
df = df.set_index('time_keys')
# 使用pivot函数将数据根据时间键进行透视,转换为表格形式
table = df.pivot(columns='name', values='datas')
# 输出表格
print(table)
```
输出结果为:
```
name A相电压 B相电压
time_keys
2023-05-25 00:00:00.000 6.079 6.079
2023-05-25 00:05:00.000 6.079 6.079
2023-05-25 00:10:00.000 6.079 6.064
2023-05-25 00:15:00.000 6.079 6.074
2023-05-25 00:20:00.000 6.079 6.065
```
阅读全文