【问题标题】:Convert two different dataframes to one json file using pandas使用 pandas 将两个不同的数据帧转换为一个 json 文件
【发布时间】:2019-08-02 18:50:33
【问题描述】:

我的第一个数据框 df_gammask 看起来像这样:

    distance breakEvenDistance  min max
0   2.1178  2.0934  NaN         0.000955
1   2.0309  2.1473  0.000955    0.001041
2   1.9801  1.7794  0.001041    0.001124
3   1.9282  2.1473  0.001124    0.001199
4   1.8518  1.5885  0.001199    0.001259
5   1.8518  1.5151  0.001259    0.001319

还有我的第二个df_gammabid

distance    breakEvenDistance   min max
0   1.9999  1.9329  NaN         0.001034
1   1.9251  2.0670  0.001034    0.001118
2   1.8802  1.6758  0.001118    0.001193
3   1.8802  1.5956  0.001193    0.001252
4   1.7542  1.5181  0.001252    0.001317
5   1.7542  1.4541  0.001317    0.001374

我需要的是有一个这样的 json 文件:

{
  "buy": [
    {
      "distance": 0.6278,
      "breakEvenDistance": 0.6261,
      "max": 0.0031920626236615754
    },
    {
      "distance": 0.6224,
      "breakEvenDistance": 0.6199,
      "min": 0.0031920626236615754,
      "max": 0.003223405873670448
    },
    {
      "distance": 0.6202,
      "breakEvenDistance": 0.6142,
      "min": 0.003223405873670448,
      "max": 0.003253791039488344
    },
    {
      "distance": 0.6174,
      "breakEvenDistance": 0.6081,
      "min": 0.003253791039488344,
      "max": 0.003285709011703031}],


"sell": [
    {
      "distance": 0.8012,
      "breakEvenDistance": 0.8005,
      "max": 0.0024962095663052064
    },
    {
      "distance": 0.7996,
      "breakEvenDistance": 0.7939,
      "min": 0.0024962095663052064,
      "max": 0.002516799325547373
    },
    {
      "distance": 0.794,
      "breakEvenDistance": 0.7877,
      "min": 0.002516799325547373,
      "max": 0.0025370182220432014
    },
    {
      "distance": 0.7927,
      "breakEvenDistance": 0.7807,
      "min": 0.0025370182220432014,
      "max": 0.0025605480833123294
    }]

我知道有函数pd.DataFrame.to_json,但它适用于一个数据帧,关于如何使用 2 个数据帧和上述格式的任何线索?我必须合并它们吗? buy 一侧是df_gammasksell 一侧是dg_gammabid!谢谢

【问题讨论】:

    标签: python json pandas


    【解决方案1】:

    在嵌套字典理解中使用DataFrame.to_dict 删除缺失值,然后创建dictionary 并转换为json

    import json
    L1 = [{k: v for k, v in x.items() if pd.notnull(v)} for x in df_gammask.to_dict('r')]
    L2 = [{k: v for k, v in x.items() if pd.notnull(v)} for x in df_gammabid.to_dict('r')]
    

    with open('file.json', 'w') as file:
        json.dump({ "buy": L1, "sell": L2}, file)
    

    【讨论】:

    • 没有Nan,实际上是我想要的,谢谢!但是变量j 的格式是什么呢?如何将此值写入本地文件?谢谢
    【解决方案2】:

    首先将您的数据帧转换为字典:

    dict_gammask = df_gammask.to_dict()
    dict_gammabid = df_gammabid.to_dict()
    

    然后你把它们放在另一个字典里,按照你想要的结构:

    result_dict = {'buy': dict_gammabid, 'sell': dict_gammask}
    

    然后你可以把它转换成json:

    import json
    json_result = json.dumps(result_dict)
    

    或将其保存到文件中:

    with open('data.json', 'w') as outfile:
        json.dump(result_dict, outfile)
    

    【讨论】:

    • Amir ,谢谢,但我怎样才能将 json_result 保存在本地 json 文件中?谢谢
    • @Viktor.w import json with open('data.json', 'w') as outfile: json.dump(data, outfile)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-27
    • 2020-10-30
    • 2018-06-15
    • 1970-01-01
    • 2021-02-23
    • 2015-08-05
    • 2022-11-27
    相关资源
    最近更新 更多