pythonpandasdictionary-comprehension

Dictionary Comprehension within pandas dataframe column


Trying to match a dictionary item with a string value from another column. sample data:

df =     A    B
     0  'a'  {'a': '2', 'b': '5'}
     1  'c'  {'a': '2', 'b': '16', 'c': '32'}
     2  'a'  {'a': '6', 'd': '23'} 
     3  'd'  {'b': '4', 'd': '76'}
     

I'm trying to get the following out:

Df =     A    B
     0   'a'  {'a': '2'}
     1   'c'  {'c': '32'}
     2   'a'  {'a': '6'}
     3   'd'  {'d': '76'}

I got this far not inside a dataframe:

d = {k: v for k, v in my_dict.items() if k == 'a'}

for a single line, but I couldn't get this to work and to be fair, I didn't expect it to work directly, but was hoping i was close:

Test_df['B'] = {k: v for k, v in test_df['B'].items() if k == test_df['A']}

I get the following error:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

What do I need to do to get this to work, or is there a better more efficient way?


Solution

  • You can use a list comprehension with zip:

    df['B'] = [{x: d[x]} for x, d in zip(df['A'], df['B'])]
    

    Output:

       A            B
    0  a   {'a': '2'}
    1  c  {'c': '32'}
    2  a   {'a': '6'}
    3  d  {'d': '76'}