Home > Net >  How to flip key and value pair of dictionary in df column, only where dictionary value starts with s
How to flip key and value pair of dictionary in df column, only where dictionary value starts with s

Time:12-06

I have a df with dictionaries in one of the columns. Some of the key value pairs within the dictionary are flipped, so when I use pd.json_normalize on the column, the keys, that are values are becoming columns. I only want to spin the keys/values for where equals test. Some dictionaries per row have multiple elements in each dictionary object.

DF looks like this:

   dict_col
0  {'aaa':'bbb'}
1  {'123':'test a'}
2  {'345':'test b','ccc':'dd','789':'test c'}

For every value in the dictionary that starts with the string 'test', I want to flip the key/value pairs like so:

   dict_col
0  {'aaa':'bbb'}
1  {'test a':'123'}
2  {'test b':'345','ccc':'dd','test c':'789'}

This code works on for one rows of the column, but I dont know how to look for the string, 'test', and run this code for only that part of the dictionary:

{v: k for k, v in df.dict_col[0].items()}

I was thinking something like:

for index, row in df.iterrows():
       (do something, but not sure what that something is)

CodePudding user response:

Can you try this:

def check_keys(dictt):
    final={}
    for x, y in dictt.items():
        if 'test' in y:
            final.update({y:x})
        else:
            final.update({x:y})
    return final
    
    
df['dict_col2']=df['dict_col'].apply(lambda x: check_keys(x))

'''
                                          dict_col                                        dict_col2
0                                   {'aaa': 'bbb'}                                   {'aaa': 'bbb'}
1                                {'123': 'test a'}                                {'test a': '123'}
2  {'345': 'test b', 'ccc': 'dd', '789': 'test c'}  {'test b': '345', 'ccc': 'dd', 'test c': '789'}

'''

Or one line:

df['dict_col3']=df['dict_col'].apply(lambda x: {k: v for d in [{v:k} if 'test' in v else {k:v} for k,v in x.items()] for k, v in d.items()})
  • Related