Creating a new column by extracting values from dict in Pandas
Looks like you have bad data (empty fields) in your source under "targets" column, because the following works:
df = pd.DataFrame([{'name': 'Bob', 'targets': {'codes':[3,4,6,199], 'region':'us', 'meta':''}}])
print(df)
# name targets
# 0 Bob {'codes': [3, 4, 6, 199], 'region': 'us', 'met...
df['targets.code'] = df['targets'].apply(lambda x: x['codes'])
print(df)
# name targets targets.code
# 0 Bob {'codes': [3, 4, 6, 199], 'region': 'us', 'met... [3, 4, 6, 199]
You can use pd.json_normalize
:
df['targets.code'] = pd.json_normalize(df['targets'])['codes']
print(df)
# Output
name targets imp targets.code
0 Bob {'codes': [3, 4, 6, 199], 'region': 'us', 'met... 200 [3, 4, 6, 199]
1 Diana {'codes': [3, 33, 199], 'region': 'us', 'meta'... 100 [3, 33, 199]
You can also use a comprehension:
df['targets.code'] = [x['codes'] if x else [] for x in df['targets']]
print(df)
# Output
name targets imp targets.code
0 Bob {'codes': [3, 4, 6, 199], 'region': 'us', 'met... 200 [3, 4, 6, 199]
1 Diana {'codes': [3, 33, 199], 'region': 'us', 'meta'... 100 [3, 33, 199]
2 Test None 50 []