Skip to content
Advertisement

Extract a value from a JSON string stored in a pandas data frame column

I have a pandas dataframe with a column named json2 which contains a json string coming from an API call:

"{'obj': [{'timestp': '2022-12-03', 'followers': 281475, 'avg_likes_per_post': 7557, 'avg_comments_per_post': 182, 'avg_views_per_post': 57148, 'engagement_rate': 2.6848}, {'timestp': '2022-12-02', 'followers': 281475, 'avg_likes_per_post': 7557, 'avg_comments_per_post': 182, 'avg_views_per_post': 57148, 'engagement_rate': 2.6848}]}"

I want to make a function that iterates over the column and extracts the number of followers if the timestp matches with a given date

JavaScript

I should get 281475 as value in the column date but I got an error: “list indices must be integers or slices, not str”

What I’m doing wrong? Thank you in advance

Advertisement

Answer

The key named obj occurs in list of dictionaries. Before you define another key, you must also specify the index of the list element.

JavaScript

Also you can use this too. This does the same job as the function you are using:

JavaScript

for list of dicts:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement