I’m working with a dataframe:
day account_id balance
2022-11-01 ncw8y7 1.000000
2022-11-02 ncw8y7 1.000424
Which I would like to transform into nested dictionary in which the 1st level is the account_id, 2nd level is category name "balances", 3rd level is the date which finally contains date as a list and balance:
{"account_id":"ncw8y7","balances":{"2022-11-01":{"day":[2022,11,01],"balance":1.00000},"2022-11-02":{"day":[2022,11,2],"balance":1.000424}}}
I tried to adopt solutions found in this and that post. However, fo some reason when using the first solution to try to learn how to do first part of this transformation (part that does not include yet a date as a list):
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}}}
I don’t get why 'day'
variable is getting formated in this way: datetime.date(2022, 11, 1)
. Is that because of getting it as index? How would you suggest me to adopt the dict
function to get to the desired effect?
2
Answers
This should work:
Output:
you can try using the to_json() method as its output is in dict format
it will provide the format u need but if you want to work on the data after the conversion better option is to_dict() I guess (source)