我是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太大,并且正在寻找像冷压力一样填充上面的动态值
答案 0 :(得分:1)
如果您像在这里那样很少或不做任何处理,则熊猫既杀伤力又太复杂。我的建议是坚持使用标准库中的csv
和json
模块。
代码可能是(或多或少):
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)
答案 1 :(得分:0)
您尝试过类似的事情
# 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'))