• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Dataframe to dictionary with list of items

Resolved: Dataframe to dictionary with list of items

0
By Isaac Tonny on 18/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I have this Dataframe:
And I am trying to create a dictionary where for the same key, there’s a list of values, like this:
I have tried to use df.apply but I couldn’t find a solution. Do you have some idea?

Answer:

Try this:
df.groupby('key')['value'].apply(list).to_dict()
Result:
{1: ['kpi1', 'kpi2', 'kpi3'], 2: ['kpi4', 'kpi5']}

If you have better answer, please add a comment about this, thank you!

apply dataframe dictionary python
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: Input Focus between two React Components

02/04/2023

Resolved: linq2db throws exception when filtering by nested collection

02/04/2023

Resolved: Table data is coming as empty in React

02/04/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.