0

I have a Dataframe in the below format:

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

Expected output:

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

1 Answer 1

1

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.

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:

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.