且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从 pandas 数据框创建列表字典

更新时间:2023-02-17 12:54:50

您可以 groupby ,汇总到列表中,返回 rec.array to_records 并根据结果构建字典:

You can groupby, aggregate to lists, return a rec.array with to_records and construct a dictionary from the result:

dict(df.groupby('Model').agg(list).to_records())
# {'Ford': ['F-150', 'Escape', 'Mustang'], 'Jeep': ['Grand Cherokee', 'Wrangler']}