数据框架到嵌套字典:Python



我正在处理一个数据框架:

day        account_id     balance
2022-11-01  ncw8y7     1.000000
2022-11-02  ncw8y7     1.000424

我想将其转换为嵌套字典,其中第一级是account_id,第二级是类别名称"余额",第三级是日期,最后包含日期作为列表和余额:

{"account_id":"ncw8y7","balances":{"2022-11-01":{"day":[2022,11,01],"balance":1.00000},"2022-11-02":{"day":[2022,11,2],"balance":1.000424}}}

我试着采用在这篇和那篇文章中找到的解决方案。然而,由于某些原因,在使用第一个解决方案时,尝试学习如何执行此转换的第一部分(尚未将日期作为列表包含在内的部分):

dict = results.groupby('account_id')[['day','balance']].apply(lambda x: x.set_index('day').to_dict(orient='index')).to_dict()

I am getting

{'ncw8y7': {datetime.date(2022, 11, 1): {'balance': 1.00000},
datetime.date(2022, 11, 2): {'balance': 1.000424}}}

我不明白为什么'day'变量以这种方式被格式化:datetime.date(2022, 11, 1)。是因为把它作为索引吗?你建议我如何使用dict函数来达到预期的效果?

应该可以:

dfx=df.groupby('account_id').agg(list)
final=[]
for i in dfx.index:
final.append({"account_id":i,"balances":{dfx['day'][i][j]:{"day":[dfx['day'][i][j]],"balance":dfx['balance'][i][j]} for j in range(len(dfx['day'][i]))}})

:

[
{
"account_id":"ncw8y7",
"balances":{
"2022-11-01":{
"day":[
"2022-11-01"
],
"balance":1.0
},
"2022-11-02":{
"day":[
"2022-11-02"
],
"balance":1.000424
}
}
}
]

您可以尝试使用to_json()方法,因为它的输出是dict格式

data.to_json('path',orient='records')

它将提供你需要的格式,但如果你想在转换后处理数据更好的选择是to_dict()我猜(源)

df.to_dict(orient=records)
orient: {‘dict’, ‘list’, ‘series’, ‘split’, ‘tight’, ‘records’, ‘index’}
Determines the type of the values of the dictionary.
‘dict’ (default) : dict like {column -> {index -> value}}
‘list’ : dict like {column -> [values]}
‘series’ : dict like {column -> Series(values)}
‘split’ : dict like {‘index’ -> [index], ‘columns’ -> [columns], ‘data’ -> [values]}
‘tight’ : dict like {‘index’ -> [index], ‘columns’ -> [columns], ‘data’ -> [values], ‘index_names’ -> [index.names], ‘column_names’ -> [column.names]}
‘records’ : list like [{column -> value}, … , {column -> value}]
‘index’ : dict like {index -> {column -> value}}

相关内容

  • 没有找到相关文章

最新更新