Home > Net >  Returning a list of dictionary keys in a dataframe
Returning a list of dictionary keys in a dataframe

Time:10-04

My dataframe:

    _id      answers                                                                                                 
    a       [{'title': 'dog', 'value': True},
            {'title': 'cat', 'value': False},                
            {'title': 'bird', 'value': False}]              
    b       [{'title': 'food', 'value': False},                   
            {'title': 'water', 'value': True}, 
            {'title': 'wine', 'value': False}]               
    c       []                                                  
                                                                 
    d       [] 

I want to build an extra column containing a list of the keys:

[dog, cat, bird]
[food, water, wine]
[]
[]          

In theory, one can use:

def getList(dict):
    return dict.keys()

But how do I iterate over the dataframe elements when in fact I have a list of dictionaries, not a dictionary?

CodePudding user response:

I think you need title values from list of dicts:

df['keys'] = df['answers'].apply(lambda x: [y['title'] for y in x])

Alternative:

df['keys'] = [[y['title'] for y in x ] for x in df['answers']]

print (df)
                                             answers                 keys
0  [{'title': 'dog', 'value': True}, {'title': 'c...     [dog, cat, bird]
1  [{'title': 'food', 'value': True}, {'title': '...  [food, water, wine]
2                                                 []                   []
3                                                 []                   []

For keys ouput is different:

df['keys'] = df['answers'].apply(lambda x: [y.keys() for y in x])
print (df)
                                             answers  \
0  [{'title': 'dog', 'value': True}, {'title': 'c...   
1  [{'title': 'food', 'value': True}, {'title': '...   
2                                                 []   
3                                                 []   

                                               keys  
0  [(title, value), (title, value), (title, value)]  
1  [(title, value), (title, value), (title, value)]  
2                                                []  
3                                                []  

CodePudding user response:

df['keys'] = df.apply(lambda row: list(row['answers'].keys()), axis=1)
  • Related