【问题标题】:How to flatten out an array from JSON into pandas如何将 JSON 中的数组展平为 pandas
【发布时间】:2017-03-16 01:31:38
【问题描述】:

我有一个 JSON:

[{
"analogData": [
[122483,104],[122493,100],[122503,106],[122513,106],[122523,107]
[122533,99],[122543,103],[122553,98],[122563,106],[122573,95],
[122583,98],[122593,97],[122603,95],[122613,101],[122623,99],
[122633,98],[122643,101],[122653,102],[122663,105],[122673,99],
[122683,102],[122693,107],[122703,106],[122713,108],[122723, 99],
[122733,98],[122743,104],[122753,104],[122763,96],[122773,99]]
"upTime": 132833,"deviceId": "5c6d27a","state": "idle"},  next 30 readings, ...]"

为下一组再次重复。

我能够将 JSON 加载到数据框中,结果如下:

... 0  [[122483, 104], [122493, 100], [122503, 106], ...
1      [[41614, 103], [41624, 105], [41634, 102], [41...
2      [[22674, 113], [22684, 89], [22694, 106], [227...
3      [[220570, 81], [220580, 81], [220590, 81], [22...
4      [[160474, 85], [160484, 86], [160494, 86], [16...

我想将“analogData”重新格式化为具有 4 列的数据框:索引、正常运行时间、时间和级别

index uptime  time    level   
1     132833  122483  104
2     132833  122493  100
3     132833  122503  106

...

【问题讨论】:

    标签: python json pandas dataframe time-series


    【解决方案1】:

    基本上,您使用 Json API 将 JSON 加载到字典中,然后根据需要提取值。

    用pythonic的方式来做:

    my_json = '{"analogData": [[122483,104],[122493,100],[122503,106],[122513,106],[122523,107],[122533,99],[122543,103],[122553,98],[122563,106],[122573,95],[122583,98],[122593,97],[122603,95],[122613,101],[122623,99],[122633,98],[122643,101],[122653,102],[122663,105],[122673,99],[122683,102],[122693,107],[122703,106],[122713,108],[122723, 99],[122733,98],[122743,104],[122753,104],[122763,96],[122773,99]], "upTime": 132833,"deviceId": "5c6d27a","state": "idle"}'
    
    buffer = json.loads(my_json)
    aD = buffer["analogData"]
    uT = buffer["upTime"]
    print( [[i+1, uT] + aD[i] for i in range(len(aD))])
    

    输出:

    [[1, 132833, 122483, 104], [2, 132833, 122493, 100], [3, 132833, 122503, 106], [4, 132833, 122513, 106], [5, 132833, 122523, 107], [6, 132833, 122533, 99], [7, 132833, 122543, 103], [8, 132833, 122553, 98], [9, 132833, 122563, 106], [10, 132833, 122573, 95], [11, 132833, 122583, 98], [12, 132833, 122593, 97], [13, 132833, 122603, 95], [14, 132833, 122613, 101], [15, 132833, 122623, 99], [16, 132833, 122633, 98], [17, 132833, 122643, 101], [18, 132833, 122653, 102], [19, 132833, 122663, 105], [20, 132833, 122673, 99], [21, 132833, 122683, 102], [22, 132833, 122693, 107], [23, 132833, 122703, 106], [24, 132833, 122713, 108], [25, 132833, 122723, 99], [26, 132833, 122733, 98], [27, 132833, 122743, 104], [28, 132833, 122753, 104], [29, 132833, 122763, 96], [30, 132833, 122773, 99]]
    

    【讨论】:

      【解决方案2】:

      假设 JSON 数据被加载到变量 data 中,也许你可以尝试一些数据结构操作?

      data = {}
      
      # load up the JSON file, assuming that's how it's saved
      with open("test.json") as json_file:
          data = json.load(json_file)  # now a Python list of dict
      
      new_data = []
      temp_data = {}  # for temporary use
      
      for col in data:  # iterate through the data
         for row in col["analogData"]:
             temp_data["uptime"] = col["upTime"]
             # split up the analogData tuple-like lists
             temp_data["analogData"], temp_data["level"] = row
             new_data.append(temp_data)
      
             temp_data = {}  # reset row
      
      df = pandas.DataFrame(new_data)
      

      不太确定你想要你的index 列,但 Pandas 在创建数据框时会使用它的默认索引列。

      【讨论】:

        猜你喜欢
        • 2020-03-11
        • 2021-04-27
        • 2018-10-01
        • 2021-04-19
        • 2019-10-24
        • 2020-09-27
        • 2021-05-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多