Home > Mobile >  How to extract value from a dict in a string - Pandas / Python
How to extract value from a dict in a string - Pandas / Python

Time:11-22

I have a pandas DataFrame with one feature, df['Computed Data'].

Computed Data
'{"stats":{"parcelTypeCount":{"1":"31","4":"31"}},"plaintsCard":[{"rootStatus":"new","plaintsCount":1,"residencyCount":1}],"parcelsCount":62,"membersStatus":{"activable":"10","activated":"18","inactivable":"3"},"assembliesCount":0,"invoicableParcelsCount":"31"}'
'{"parcelsCount":33,"invoicableParcelsCount":"11","stats":{"parcelTypeCount":{"1":"9","4":"22","11":"2"}},"plaintsCard":[],"assembliesCount":0,"membersStatus":{"activated":"0","activable":"9","inactivable":"1"}}'
'{"parcelsCount":79,"invoicableParcelsCount":"32","stats":{"parcelTypeCount":{"1":"29","4":"32","18":"3","23":"15"}},"plaintsCard":[],"assembliesCount":0,"membersStatus":{"activated":"0","activable":"28","inactivable":"2"}}'
'{"parcelsCount":80,"invoicableParcelsCount":"32","stats":{"parcelTypeCount":{"1":"31","4":"42","13":"1","23":"6"}},"plaintsCard":[],"assembliesCount":0,"membersStatus":{"activated":"0","activable":"27","inactivable":"6"}}'
'{"stats": {"parcelTypeCount": {"17": "27"}}, "plaintsCard": [], "parcelsCount": 27, "membersStatus": {"activable": "9", "activated": "2", "inactivable": "16"}, "assembliesCount": 0, "invoicableParcelsCount": "0"}'

I want to extract the "membersStatus", "activable" part from every string and to put it in a new column.

I have tried to use ast.literal_eval() and it is working but only when I apply it to one value

x = ast.literal_eval(df["Computed Data"][0])
x["membersStatus"]["activable"]
'10'

It gives me : '10'. Which is what I want but for every dict in "Computed Data" and to put it in a new column.

I tried to do it with a for loop :

for n, i in enumerate(df["Computed Data"]):
    x = ast.literal_eval(df["Computed Data"][n])
ValueError: malformed node or string: <_ast.Name object at 0x13699c610>

I don't know how can I change what I did to make it work.

Can you Help please ?

CodePudding user response:

you can use apply methods to extract them:

df['member Status']=df['Computed Data'].apply(lambda x:eval(x)['membersStatus']['activable'])

CodePudding user response:

you can use:

import json
df['Computed Data']=df['Computed Data'].apply(json.loads)
df['activable']=df['Computed Data'].apply(lambda x: x['membersStatus']['activable'])

if there are nan values:

df['Computed Data']=df['Computed Data'].apply(lambda x: json.loads(x) if pd.notna(x) else np.nan)
df['activable']=df['Computed Data'].apply(lambda x: x['membersStatus']['activable'] if pd.notna(x) else np.nan)
  • Related