Extracting a value from a dictionary within a list

Question:

I have a DF column that is a dictionary within a list. I want to create a new column with the ‘value’ from value in the dictionary i.e. ‘IT-Sourced Changes 2022’, but with the below, I’m getting NaN values.

import pandas as pd

data = [10,[{'self': 'https://elia.atlassian.net/rest/api/3/customFieldOption/10200', 'value': 'IT-Sourced Changes 2022', 'id': '10200'}],30]
df = pd.DataFrame(data, columns=['Data'])
df['new_col']= df['Data'].str['value']
df.head(3)
Asked By: elia_Werner

||

Answers:

As you have a list of a dictionary, you need to explode it first:

df.Data.explode().str['value']
Answered By: Nuri Taş
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.