• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Converting list of dictionaries into Dataframe in sane way

Resolved: Converting list of dictionaries into Dataframe in sane way

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

Question:

The zendesk api returns fields as a list of dictionaries but each list is a single record. I’m wondering if there is a better way to turn it all into a dataframe. If it’s a dictionary of dictionaries then json_normalize takes care of it with no problem.
Caveat: not all records will have the same field IDs
Sample data:
Running any form of Dataframe, from_records, from_json, or json_normalizes gives most of what I want but with the list in a single column:
Output:
ticket_id customer_id created_at custom_fields group_id
0 4 8 2022-05-01 [{‘id’: 15, ‘value’: ‘website’}, {‘id’: 16, ‘v… 42

My current, probably ill-advised, solution is:
This results in a correct record that I could append to a main dataframe:
ticket_id customer_id created_at group_id 15 16 23
0 4 8 2022-05-01 42 website broken None

My worry is that i need to do this to ~25,000 records and this seems like it will be both slow and brittle (prone to breaking).

Answer:

You should wrangle the data/dictionary first and only then construct a DataFrame with it. It will make your life easier and is faster than trying to manipulate the data with pandas i.e. after the DataFrame is created.
Output:

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

dataframe pandas python
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: PyCharm cannot see my newly compiled .pyc see on import

27/03/2023

Resolved: I am facing ERR_HTTP2_PROTOCOL_ERROR on my website

27/03/2023

Resolved: TypeScript does not recognize properties when instantiating interface array

27/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

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