1

I have a Dataframe in the below format:

id, ref
101, [{'id': '74947', 'type': {'id': '104', 'name': 'Sales', 'inward': 'Sales', 'outward': 'PO'}, 'inwardIssue': {'id': '76560', 'key': 'Prod-A'}}]
102, [{'id': '74948', 'type': {'id': '105', 'name': 'Return', 'inward': 'Return Order', 'outward': 'PO'}, 'inwardIssue': {'id': '76560', 'key': 'Prod-C'}}]
103, [{'id': '74949', 'type': {'id': '106', 'name': 'Sales', 'inward': 'Return Order', 'outward': 'PO'}, 'inwardIssue': {'id': '76560', 'key': 'Prod-B'}}]

I am trying to extract rows that have name = Sales and return back the below output:

id, value
101, Prod-A 
103, Prod-B

1 Answer 1

2

Use str[0] for first lists with Series.str.get by values by keys of dicts:

#if necessary convert list/dict repr to list/dict
import ast
df['ref'] = df['ref'].apply(ast.literal_eval)

df['names'] = df['ref'].str[0].str.get('type').str.get('name')
df['value'] = df['ref'].str[0].str.get('inwardIssue').str.get('key')

print (df)

    id                                                ref   names   value
0  101  [{'id': '74947', 'type': {'id': '104', 'name':...   Sales  Prod-A
1  102  [{'id': '74948', 'type': {'id': '105', 'name':...  Return  Prod-C
2  103  [{'id': '74949', 'type': {'id': '106', 'name':...   Sales  Prod-B

And then filter by boolean indexing:

df1 = df.loc[df['names'].eq('Sales'), ['id','value']]
print (df1)
    id   value
0  101  Prod-A
2  103  Prod-B
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the reply.. I however get this error AttributeError: 'str' object has no attribute 'values'

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.