转换JSON到大熊猫数据帧
我有这样的JSON数据:转换JSON到大熊猫数据帧
{ "current": [
[
0,
"2017-01-15T00:08:36Z"
],
[
0,
"2017-01-15T00:18:36Z"
]
],
"voltage": [
[
12.891309987,
"2017-01-15T00:08:36Z"
],
[
12.8952162966,
"2017-01-15T00:18:36Z"
]
]
}
,我试图进入它进入这个格式(时间序列)一个大熊猫数据帧:
time current voltage 2017-01-15T00:08:36Z 0 12.891309987
2017-01-15T00:18:36Z 0 12.8952162966
我有尝试:
t = pd.read_json(q)
,但是这给了我:
current voltage 0 [0, 2017-01-15T00:08:36Z] [12.891309987, 2017-01-15T00:08:36Z]
1 [0, 2017-01-15T00:18:36Z] [12.8952162966, 2017-01-15T00:18:36Z]
我怎样才能把它变成正确的格式?
回答:
如果两列时间是一样的,aftering阅读JSON,我们可以选择的值和Concat的他们:
ndf = pd.read_json(q) ndf = pd.concat([ndf.apply(lambda x : x.str[0]),ndf['current'].str[1].rename('time')],1)
current voltage time
0 0 12.891310 2017-01-15T00:08:36Z
1 0 12.895216 2017-01-15T00:18:36Z
回答:
据我所知,还没有在read_json选项()来做到这一点。我的建议是在您读取数据后重新操作表格。
t = pd.read_json('data.json') t['time'] = [x[1] for x in t['current']]
t['current'] = [x[0] for x in t['current']]
t['voltage'] = [x[0] for x in t['voltage']]
以上是 转换JSON到大熊猫数据帧 的全部内容, 来源链接: utcz.com/qa/265348.html