Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I define a function to extract values from nested dictionary for each row in python

I have a column named ‘urls’ in dataframe ‘df’ that each row consists of nested dictionaries with a URL and whether it is malicious or not. I’d like to extract only the value of the nested dictionary for each row.

0    {'url example 1': {'malicious': False}}
1    {'url example 2': {'malicious': False}}  

By defining a function, I’d like to use ‘apply’ function to get the result for each row.

Here’s the sample function that I have defined.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

def urlconcern(url):
    try:
        r = s.lookup_urls([url]) 
        return r.values()
    except:
        pass

After running this with ‘apply’ function

df['urls'].apply(urlconcern)

This only gives the result below with round bracket (strangely)

0    ({'malicious': False})
1    ({'malicious': False})

The desired answer would be

False
False

Could there be any way to do so?

>Solution :

Given pandas series s (I’m assuming it’s a pandas series)

s = pd.Series([{'url example 1': {'malicious': False}},
               {'url example 2': {'malicious': False}}])

you can use generator expression inside next to look for values of nested dicts.

out = s.apply(lambda url: next((v for d in url.values() for k,v in d.items()), None))

Output:

0    False
1    False
dtype: bool

However, I’m not convinced this is what you’re looking for since you’re losing the url info here.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading