【问题标题】:custom nested json structre with different sets具有不同集合的自定义嵌套 json 结构
【发布时间】:2019-09-18 10:38:23
【问题描述】:

我是 python 新手,pandas 有一个 csv 文件

.----.---------.-------.-------------------.-------------------.-------------------.-------------------.
| id | country | state | cold_stress_score | cold_stress_level | heat_stress_score | heat_stress_level |
:----+---------+-------+-------------------+-------------------+-------------------+-------------------:
|  1 | USA     | NJ    |             0.003 | low               |             0.673 | moderate          |
:----+---------+-------+-------------------+-------------------+-------------------+-------------------:
|  2 | USA     | NJ    |             0.001 | high              |               0.2 | high              |
:----+---------+-------+-------------------+-------------------+-------------------+-------------------:
|  3 | USA     | NJ    |             0.004 | moderate          |               0.3 | low               |
:----+---------+-------+-------------------+-------------------+-------------------+-------------------:
|  4 | USA     | NY    |             0.005 | moderate          |               0.4 | moderate          |
:----+---------+-------+-------------------+-------------------+-------------------+-------------------:
|  5 | USA     | NY    |             0.006 | high              |               0.5 | high              |
:----+---------+-------+-------------------+-------------------+-------------------+-------------------:
|  6 | USA     | NY    |             0.009 | low               |               0.6 | low               |
'----'---------'-------'-------------------'-------------------'-------------------'-------------------'

我想把它转换成 json 的嵌套方式

预期的 json

  {
  "id":1,
  "country": "USA",
  "state": "NJ",
  "cold_stress":{
    "cold_stress_score" : 0.003,
    "cold_stress_level": "low",
  },
  "heat_stress":{
    "heat_stress_score" : 0.0673,
    "heat_stress_level": "moderate",

  }

}

我试过这个解决方案 Convert Pandas Dataframe to nested JSON

j = (df.groupby(['id','country','state'], as_index=False)
             .apply(lambda x: x[['cold_stress_score','cold_stress_level']].to_dict('r'))
             .reset_index()
             .rename(columns={0:'cold_stress'})
             .to_json(orient='records'))

我想给 json 添加热应力 以上代码返回

  "id":1,
  "country": "USA",
  "state": "NJ",
  "cold_stress":{
    "cold_stress_score" : 0.003,
    "cold_stress_level": "low",
  }
}

我怎样才能添加 heat_stress 我的 csv 太大了,我正在寻找填充在上面的动态值,比如冷应力

【问题讨论】:

    标签: python json python-3.x pandas pandas-groupby


    【解决方案1】:

    如果您像这里那样做很少或不做任何处理,pandas 既矫枉过正又过于复杂。我的建议是坚持使用标准库中的 csvjson 模块。

    代码可能(或多或少):

    with open(inputfile) as fdin, open (outputfile, "w") as fdout:
        rd = csv.DictReader(fdin)
        js = [{'id': int(row['id']), 'country': row['country'], 'state': row['state'],
               'cold_stress': {'cold_stress_code': row['cold_stress_code'],
                               'cold_stress_level': row['cold_stress_level']},  
               'heat_stress': {'heat_stress_code': row['heat_stress_code'],
                               'heat_stress_level': row['heat_stress_level']}, 
               } for row in rd]
        json.dump(js, fdout, indent=2)
    

    【讨论】:

    • 我必须对数据进行一些过滤,例如基于 id 或 state 或 country 的选择,我认为 pandas 最好进行更多操作和过滤
    【解决方案2】:

    你有没有尝试过类似的东西

    # create first grouping (leave heat columns same)
    j = (df.groupby(['id','country','state', 'heat_stress_score', 'heat_stress_level'], as_index=False)
                 .apply(lambda x: x[['cold_stress_score','cold_stress_level']].to_dict('r'))
                 .reset_index()
                 .rename(columns={0:'cold_stress'}))
    # care about heat grouping
    j = (j.groupby(['id','country','state', 'cold_stress'], as_index=False)
                 .apply(lambda x: x[['heat_stress_score','heat_stress_level']].to_dict('r'))
                 .reset_index()
                 .rename(columns={0:'heat_stress'})
                 .to_json(orient='records'))
    
    

    【讨论】:

    • 不,它不工作它只返回tide-Data第二个不来
    • 我刚查了renamesee here的文档,调整了答案。
    • 似乎它不起作用,我分享的代码片段只是示例,我需要添加 heat_stress 作为第二个,但示例适用于潮数据,我想添加不同的潮数据列名
    • 修改了答案以反映您对 OP 的更改。如果您提供一个最小的示例(包括创建 10 行数据框),帮助您会容易得多。
    • 在第一个 groupby 之后添加第二个它返回错误 TypeError: unhashable type: 'list'
    猜你喜欢
    • 1970-01-01
    • 2023-03-06
    • 2021-12-11
    • 2014-10-10
    • 2015-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-27
    相关资源
    最近更新 更多