Creating a dictionary of lists from pandas data frame

后端 未结 1 557
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-16 04:43

I\'m trying to create a dictionary of lists based on a pandas dataframe, I need a dictionary of lists to pass for my Plotly dashboard

In:
df.head()
Model             


        
相关标签:
1条回答
  • 2021-01-16 04:52

    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']}
    
    0 讨论(0)
提交回复
热议问题