Daniel Arges
Daniel Arges

Reputation: 365

make a correct dictionary from dataframe groupby()

Take this groupby structure:

max_time = df_deh.groupby(['client_id','acc_number'])['time'].max()

This is its format:

client_id  acc_number
0          885020       2019-12-12 19:38:00
           889942       2020-12-21 14:31:39
1          896967       2020-12-15 16:25:52
2          913756       2020-11-11 15:18:17
Name: time, dtype: datetime64[ns]

I need to make a dictionary from it. The keys should be the merge of client_id and acc_number values (preferably with a - in between), and the key items, the datetime values.

Upvotes: 1

Views: 31

Answers (1)

PieCot
PieCot

Reputation: 3639

You can use to_dict and then create the keys in the format you want:

{f'{k[0]}-{k[1]}': v for k, v in max_time.to_dict().items()}

{'0-885020': Timestamp('2019-12-12 19:38:00'), '0-889942': Timestamp('2020-12-21 14:31:39'), '1-896967': Timestamp('2020-12-15 16:25:52'), '2-913756': Timestamp('2020-11-11 15:18:17')}

Upvotes: 2

Related Questions