【问题标题】:Converting pandas dataframe to custom JSON format (then to JS object)将 pandas 数据框转换为自定义 JSON 格式(然后转换为 JS 对象)
【发布时间】:2017-07-12 20:52:45
【问题描述】:

我想使用平行坐标图绘制某些分析的结果。找到了一个使用 protovis http://mbostock.github.io/protovis/ex/cars.html 制作的精彩示例,我正在尝试重新排列我的数据以根据示例 (cars.js) 复制数据文件的结构。 因此,我的dataframe 结构:

                    north   ch  wwr  ach  tmin  tmax  B1_EMS_DH26
Job_ID                                                           
EP_P1203_000000000    0.0  2.5   40  4.0    24    25       1272.2
EP_P1203_000000001    0.0  2.5   40  4.0    24    26       1401.9
EP_P1203_000000002    0.0  2.5   40  4.0    24    27       1642.3

应该转换成如下:

var results = [{
    name: "EP_P1203_000000000",
    north: 0.0,
    ch: 2.5,
    wwr: 40,
    ach: 4.0,
    tmin: 24,
    tmax: 25,
    origin: 1272.2
  },
  {
    name: "EP_P1203_000000001",
    north: 0.0,
    ch: 2.5,
    wwr: 40,
    ach: 4.0,
    tmin: 24,
    tmax: 26,
    origin: 1401.9
  },
  {
    name: "EP_P1203_000000002",
    north: 0.0,
    ch: 2.5,
    wwr: 40,
    ach: 4.0,
    tmin: 24,
    tmax: 27,
    origin: 1272.3
  },
  {
    name: "EP_P1203_000000003",
    north: 0.0,
    ch: 2.5,
    wwr: 40,
    ach: 4.0,
    tmin: 24,
    tmax: 28,
    origin: 1642.3
  },
];

除了将我的列 B1_EMS_DH26 替换为 origin(图表似乎使用它来设置线条颜色)之外,我不想手动分割行和替换符号。

使用dataframe.to_json 命令返回一行:

{
  "EP_P1203_000000000": {
    "north": 0.0,
    "ch": 2.5,
    "wwr": 40,
    "ach": 4.0,
    "tmin": 24,
    "tmax": 25,
    "B1_EMS_DH26": 1272.2
  },
  "EP_P1203_000000001": {
    "north": 0.0,
    "ch": 2.5,
    "wwr": 40,
    "ach": 4.0,
    "tmin": 24,
    "tmax": 26,
    "B1_EMS_DH26": 1401.9
  },
  "EP_P1203_000000002": {
    "north": 0.0,
    "ch": 2.5,
    "wwr": 40,
    "ach": 4.0,
    "tmin": 24,
    "tmax": 27,
    "B1_EMS_DH26": 1642.3
  }
}

这仍然不太正确。 你建议怎么做?

【问题讨论】:

    标签: javascript python json pandas parallel-coordinates


    【解决方案1】:

    您的 DataFrame(用于娱乐目的):

    df= pd.DataFrame(
        {'north': [0.0, 0.0, 0.0],
         'B1_EMS_DH26': [1272.2, 1401.9, 1642.3],
         'tmax': [25, 26, 27],
         'wwr': [40, 40, 40],
         'ch': [2.5, 2.5, 2.5],
         'tmin': [24, 24, 24],
         'ach': [4.0, 4.0, 4.0]
         },
        index=['EP_P1203_000000000', 'EP_P1203_000000001', 'EP_P1203_000000002'],
        columns=['north', 'ch', 'wwr', 'ach', 'tmin', 'tmax', 'B1_EMS_DH26'])
    

    这可能是最糟糕的方法,但它确实有效(我认为):

    import re
    import json
    
    with open('whatever.json', 'w') as f:
        f.write('var results = [\n')
        for k,v in df.drop('B1_EMS_DH26', axis=1).T.to_dict().items():
            f.write("{name:"+json.dumps(k)+", "+re.sub(r'[{"\']', '', json.dumps(v))+',\n')
        f.write('];')
    

    生产:

    var results = [{
        name: "EP_P1203_000000001",
        ach: 4.0,
        north: 0.0,
        tmax: 26.0,
        tmin: 24.0,
        ch: 2.5,
        wwr: 40.0
      },
      {
        name: "EP_P1203_000000000",
        ach: 4.0,
        north: 0.0,
        tmax: 25.0,
        tmin: 24.0,
        ch: 2.5,
        wwr: 40.0
      },
      {
        name: "EP_P1203_000000002",
        ach: 4.0,
        north: 0.0,
        tmax: 27.0,
        tmin: 24.0,
        ch: 2.5,
        wwr: 40.0
      },
    ];
    

    它将以我认为您正在寻找的结构输出一个文件。如果没有,请告诉我。我知道可怕的黑客攻击。具有高级 json 经验的人无疑知道更好的方法。

    【讨论】:

    • 谢谢你,现在我只需要取出' 符号
    • 好吧,这可能看起来很琐碎,但是'" 等符号的使用似乎对.js 语法非常敏感。我尝试在您的脚本后执行替换以进行附加,但没有成功。本质上,我需要'EP_P1203_000000001' -> "EP_P1203_000000001"'EP_P1203_000000002' -> "EP_P1203_000000002" 和所有其他' 消失(即'ach' -> ach'north' -> north 等.)
    • 我修改了我的答案。我不确定您需要什么格式,或者您是否从控制台打印它。我把它写到文件中。另外,列顺序重要吗?
    • 我想出了这个awful solution。你的可能对你来说是“最糟糕的方式”,但它对我来说是完美的!你让这个人很开心!
    猜你喜欢
    • 1970-01-01
    • 2019-09-15
    • 2015-05-16
    • 2018-10-27
    • 2019-05-14
    • 2023-03-11
    • 2019-07-01
    • 2021-12-13
    • 2021-09-27
    相关资源
    最近更新 更多