简体   繁体   English

从存储在 pandas 数据框列中的 JSON 字符串中提取一个值

[英]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:我有一个 pandas dataframe 和一个名为json2的列,其中包含来自 API 调用的 json 字符串:

"{'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我想制作一个 function 迭代该列并在timestp与给定日期匹配时提取关注者数量

def get_followers(x):
    if x['obj']['timestp']=='2022-12-03':
        return x['obj']['followers']

df['date'] = df['json2'].apply(get_followers)

I should get 281475 as value in the column date but I got an error: "list indices must be integers or slices, not str"我应该在日期列中得到 281475 作为值,但出现错误:“列表索引必须是整数或切片,而不是 str”

What I'm doing wrong?我做错了什么? Thank you in advance先感谢您

The key named obj occurs in list of dictionaries.名为obj的键出现在字典列表中。 Before you define another key, you must also specify the index of the list element.在定义另一个键之前,您还必须指定列表元素的索引。

import ast
df['json2']=df['json2'].apply(ast.literal_eval) #if dictionary's type is string, convert to dictionary.

def get_followers(x):
    if x['obj'][0]['timestp']=='2022-12-03':
        return x['obj'][0]['followers']

df['date'] = df['json2'].apply(get_followers)

Also you can use this too.你也可以使用这个。 This does the same job as the function you are using:这与您正在使用的 function 的作用相同:

df['date'] = df['json2'].apply(lambda x: x['obj'][0]['followers'] if x['obj'][0]['timestp']=='2022-12-03' else None)

for list of dicts:对于字典列表:

def get_followers(x):
    for i in x['obj']:
        if i['timestp'] == '2022-12-03':
            return i['followers']
            break
    
df['date'] = df['json2'].apply(get_followers)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM