pandas 如何解析json文件并转换为时间序列

brc7rcf0  于 2022-11-05  发布在  其他
关注(0)|答案(1)|浏览(108)

我有这个json文件:

{
  "walk": [
    {
      "date": "2021-01-10",
      "duration": 301800,
      "levels": {
        "data": [
          {
            "timestamp": "2021-01-10T13:16:00.000",
            "level": "slow",
            "seconds": 360
          },
          {
            "timestamp": "2021-01-10T13:22:00.000",
            "level": "moderate",
            "seconds": 2940
          },
          {
            "dateTime": "2021-01-10T14:11:00.000",
            "level": "fast",
            "seconds": 300

并且我想要解析该数据,以使其成为1分钟级别的时间序列数据。(即:6个数据点(360秒= 6分钟)作为水平“慢”。

timestamp              level   
2021-01-10 13:16:00     slow
2021-01-10 13:17:00     slow
.......
2021-01-10 13:22:00     moderate

我现在有权利:

with open('walks.json') as f:
df = pd.json_normalize(json.load(f), 
                       record_path=['walk']
                      )

但它返回的是嵌套在一个单元格中的每一天的级别。

nr9pn0ug

nr9pn0ug1#

需要嵌套record_path级别

df = pd.json_normalize(data=data, record_path=["walk", ["levels", "data"]])

相关问题