Need to group rows by the File_Number which will act as the dictionaries key, and have the Action_ID and Action_Note as an object.
DataFrame
**File_Number** **Action_ID** **Action_Note**
0 12 Call Josh
1 15 Text Emily
1 16 Email Guy
2 19 Visit Hannah
2 20 Call Ryan
Expected Output
{
0: [Action_ID: 12, Action_Note: Call Josh ],
1: [Action_ID: 15, Action_Note: Text Emily], [Action_ID: 16, Action_Note: Email Guy],
2: [Action_ID: 19, Action_Note: Visit Hannah], [Action_ID: 20, Action_Note: Call Ryan],
}
You can do to_dict
twice:
(df.drop(['File_Number'], axis=1)
.groupby(df['File_Number']).apply(lambda x: x.to_dict('records'))
.to_dict()
)
Output:
{0: [{'Action_ID': 12, 'Action_Note': 'Call Josh'}],
1: [{'Action_ID': 15, 'Action_Note': 'Text Emily'},
{'Action_ID': 16, 'Action_Note': 'Email Guy'}],
2: [{'Action_ID': 19, 'Action_Note': 'Visit Hannah'},
{'Action_ID': 20, 'Action_Note': 'Call Ryan'}]}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.