我稍微修改了您的JSON字符串,并添加了一条记录,使其具有不同的”Hour”组。在import pandas as pd
import numpy as np
import json
jsondata = ”'{
“data”: [
“time”: “2015-10-14 15:01:10”,
“values”: {
“d1”: 3956.58,
“d2”: 0,
“d3”: 19,
“d4”: 6.21,
“d5”: 105.99,
“d6”: 42,
“d7”: 59.24
“time”: “2015-10-14 15:01:20”,
“values”: {
“d1”: 3956.58,
“d2”: 0,
“d3”: 1,
“d4”: 0.81,
“d5”: 121.57,
“d6”: 42,
“d7”: 59.24
“time”: “2015-10-14 16:01:20”,
“values”: {
“d1”: 31956.58,
“d2”: 0,
“d3”: 1,
“d4”: 0.81,
“d5”: 121.57,
“d6”: 42,
“d7”: 59.24
”’
data = json.loads(jsondata)[‘data’]
If your JSON data is in a file, then do:
data = json.load(jsonfile)[‘data’]
df = pd.DataFrame(data=[record[‘values’] for record in data],
index=pd.DatetimeIndex([record[‘time’] for record in data], name=’time’))
print df
print df.groupby(pd.Grouper(freq=’H’)).agg([np.mean, max, min])
输出(df):
^{pr2}$
输出统计:d1 d2 d3 \
mean max min mean max min mean max min
time
2015-10-14 15:00:00 3956.58 3956.58 3956.58 0 0 0 10 19 1
2015-10-14 16:00:00 31956.58 31956.58 31956.58 0 0 0 1 1 1
d4 … d5 d6 \
mean … min mean max min mean max min
time …
2015-10-14 15:00:00 3.51 … 0.81 113.78 121.57 105.99 42 42 42
2015-10-14 16:00:00 0.81 … 0.81 121.57 121.57 121.57 42 42 42
mean max min
time
2015-10-14 15:00:00 59.24 59.24 59.24
2015-10-14 16:00:00 59.24 59.24 59.24
[2 rows x 21 columns]
直接使用pd.read_json似乎不起作用,因为生成的数据帧具有难以使用的意外结构。在
Original: https://blog.csdn.net/weixin_31083103/article/details/113966684
Author: 飞上九天
Title: python 读取json 写入csv_从Python中的JSON文件读取并写入CSV
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/743467/
转载文章受原作者版权保护。转载请注明原作者出处!