简体   繁体   English

Pandas - 从 Dataframe 列中提取值

[英]Pandas - Extracting values from a Dataframe column

I have a Dataframe in the below format:我有一个 Dataframe 格式如下:

cust_id, cust_details
101, [{'self': 'https://website.com/rest/api/2/customFieldOption/1', 'value': 'Type-A', 'id': '1'}, 
      {'self': 'https://website.com/rest/api/2/customFieldOption/2', 'value': 'Type-B', 'id': '2'}, 
      {'self': 'https://website.com/rest/api/2/customFieldOption/3', 'value': 'Type-C', 'id': '3'}, 
      {'self': 'https://website.com/rest/api/2/customFieldOption/4', 'value': 'Type-D', 'id': '4'}]
102, [{'self': 'https://website.com/rest/api/2/customFieldOption/5', 'value': 'Type-X', 'id': '5'}, 
      {'self': 'https://website.com/rest/api/2/customFieldOption/6', 'value': 'Type-Y', 'id': '6'}]

I am trying to extract for every cust_id all cust_detail values我正在尝试为每个 cust_id 提取所有 cust_detail 值

Expected output:预期 output:

cust_id, new_value
101,Type-A, Type-B, Type-C, Type-D
102,Type-X, Type-Y

Easy answer:简单的回答:

df['new_value'] = df.cust_details.apply(lambda ds: [d['value'] for d in ds])

More complex, potentially better answer:更复杂,可能更好的答案:

Rather than storing lists of dictionaries in the first place, I'd recommend making each dictionary a row in the original dataframe.我建议不要首先存储字典列表,而是在原始 dataframe 中将每个字典设为一行。

df = pd.concat([
        df['cust_id'], 
        pd.DataFrame(
            df['cust_details'].explode().values.tolist(), 
            index=df['cust_details'].explode().index
        )
     ], axis=1)

If you need to group values by id, you can do so via standard groupby methods:如果需要按 id 对值进行分组,可以通过标准的 groupby 方法进行:

df.groupby('cust_id')['value'].apply(list)

This may seem more complex, but depending on your use case might save you effort in the long-run.这可能看起来更复杂,但根据您的用例,从长远来看可能会节省您的精力。

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

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