[英]Group python dataframe and display all correspond values for each unique key in a dictionary
I have the following dataset我有以下数据集
id ![]() |
date![]() |
---|---|
7510 ![]() |
15 Jun 2020 ![]() |
7510 ![]() |
16 Jun 2020 ![]() |
7512 ![]() |
15 Jun 2020 ![]() |
7512 ![]() |
07 Jul 2020 ![]() |
7520 ![]() |
15 Jun 2020 ![]() |
7520 ![]() |
16 Aug 2020 ![]() |
I need to convert this to a dictionary which is quite straight forward, but need each unique id as a key and all corresponding values as values to the unique key.我需要将其转换为一个非常简单的字典,但需要每个唯一 id 作为键,所有对应的值作为唯一键的值。
for example;例如; dictionary = {7510: ["15 Jun 2020", "16 Jun 2020"], 7512: ["15 Jun 2020", "07 Jul 2020"], 7520: ["15 Jun 2020", "16 Aug 2020"] }
字典 = {7510:[“2020 年 6 月 15 日”、“2020 年 6 月 16 日”]、7512:[“2020 年 6 月 15 日”、“2020 年 7 月 7 日”]、7520:[“2020 年 6 月 15 日”、“2020 年 8 月 16 日”] }
Try this:尝试这个:
df.groupby('id')['date'].agg(list).to_dict()
Output: Output:
{7510: ['15 Jun 2020', '16 Jun 2020'],
7512: ['15 Jun 2020', '07 Jul 2020'],
7520: ['15 Jun 2020', '16 Aug 2020']}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.