繁体   English   中英

将json转换为pandas dataframe

[英]convert json into pandas dataframe

我有这个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]

如何将其转换为正确的格式?

如果两列时间都相同,则在读取json之后,我们可以选择值并将它们连接起来:

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']]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM