Home > Back-end >  map value of nested dictionary to new column in dataframe in python
map value of nested dictionary to new column in dataframe in python

Time:02-27

I have the following nested dictionary for each row in a DataFrame.

dictionary
{'https://twitter.com/tonythehuff/status/927186407789137920/photo/1': {'malicious': False}}
{'http://giveaway.amazon.com\\__CONNECTIONPOOL_ERROR__': {'malicious': False}}
{'http://pcktpro.com\\__CONNECTIONPOOL_ERROR__': {'malicious': True}}

I'd like to create a new column by mapping the value of the nested dictionary. The rows from the new column named 'malicious' look like

malicious
False
False
True

Would there be any efficient way to get the value of the nested dictionary as above?

Data:

{'dictionary': [
    {'https://twitter.com/tonythehuff/status/927186407789137920/photo/1': {'malicious': False}},
    {'http://giveaway.amazon.com\\__CONNECTIONPOOL_ERROR__': {'malicious': False}},
    {'http://pcktpro.com\\__CONNECTIONPOOL_ERROR__': {'malicious': True}}]}

CodePudding user response:

Looks like a list comprehension with a double loop should do the job:

df['malicious'] = [d.get('malicious') for outer_d in df['dictionary'] for d in outer_d.values()]

or you can try the following code that creates a DataFrame out of the "dictionary" column and gets the value under the "malicious" key using str.get:

df['malicious'] = (pd.DataFrame(df['dictionary'].tolist()).set_axis([0]*len(df), axis=1)
                   .groupby(level=0, axis=1)
                   .first()[0]
                   .str.get('malicious'))

Output:

                                          dictionary  malicious
0  {'https://twitter.com/tonythehuff/status/92718...      False
1  {'http://giveaway.amazon.com\__CONNECTIONPOOL_...      False
2  {'http://pcktpro.com\__CONNECTIONPOOL_ERROR__'...       True
  • Related