Dictionary Comprehension within pandas dataframe column

92 Views Asked by At

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?

3

There are 3 best solutions below

9
mozway On BEST ANSWER

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'}
0
Pep_8_Guardiola On

You can do it simply and efficiently within pandas itself using the following:

df['B'] = df.apply(lambda x: {x[0]: x[1][x[0]]}, axis=1)

Output:

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

Note that there is no error checking for if a key does not exist

0
MoRe On
class MyDict():
    def __init__(self, d: dict) -> None:
        self.dict = d

    def __sub__(self, other):
        return {x: self.dict[x] for x in other}
    
df.B.map(MyDict) - df.A