【发布时间】:2023-03-04 09:32:01
【问题描述】:
我正在尝试将 python 字典转换为 json 。
输入数据框如下所示:
user_id |last_update_time |states| tile | update_time | message
11 |1571737828 |S231 |{'Action1': [200, 250, 300],|1571737828 | traffic
'Action2': [150, 200, 400]}
22 |1571737828 |S450 |{'Action1': [100, 150, 400],|1571737828 | traffic
'Action2': [350, 500, 700]}
已经尝试过了,但是它没有提供所需的输出格式,如下所示的预期json输出格式
adf = input_df.groupby('user_id').apply(lambda x: x.set_index('message').to_dict(orient='index')).to_dict()
预期的 json 输出文件:
{
11:{
"states": "s123", "last_update_time":111342342342,
"tile": {
"Action1": [200, 250, 300],
"Action2": [150, 200, 400]
}, "update_time":111342342342
},
22:{
"states": "s124", "last_update_time":1113423442342,
"tile": {{
"Action1": [100, 150, 400],
"Action2": [350, 500, 700]
}}, "update_time":1141342342342
},
"message":"traffic"
}
这个输出的解决方案对我很有帮助。
【问题讨论】:
-
我猜你正在使用 Pandas 数据框?如果是这样,您是否尝试过此功能? pandas.pydata.org/pandas-docs/stable/reference/api/…
标签: python json dataframe dictionary