Check if int exists in dictionary in Pandas column

Question:

How do I check if multiple integers exists in a dictionary stored as a string in a columns in pandas dataframe?

Input dataframe

| player |           qualifier          |
| ------ | ---------------------------- |
| John   | "{120: 'left', 107: True}"   |
| Felix  | {1: 'box centre', 120: 5.6}" |

Check if 5, 6 and 107 exists as a key in the qualifier column

Desired output dataframe


| player |         qualifier             | set_piece |
| ------ | ----------------------------- | --------- |
| John   | "{120: 'left', 107: True}"    | True      |
| Felix  | "{1: 'box centre', 120: 5.6}" | False     |

Answers:

you can use apply with a lambda function.

values=[5,6,107]

import ast
df['qualifier']=df['qualifier'].apply(ast.literal_eval)

#Let's take the keys in the dictionary for that line as a list and compare it with the list named values. Return True if any value matches, False if not.

df['set_piece']=df['qualifier'].apply(lambda x: True if any(x in values for x in list(x.keys())) else False)
Answered By: Clegane
lst = {5,6,107}
df = pd.DataFrame({'player': ['John', 'Felix'], 'qualifier': ['{120: "left", 107: True}', '{1: "box centre", 120: 5.6}']})
df['set_piece'] = df.qualifier.apply(lambda x: True if lst.intersection(eval(x).keys()) else False)
print(df)
  player                    qualifier  set_piece
0   John     {120: "left", 107: True}       True
1  Felix  {1: "box centre", 120: 5.6}      False
Answered By: Алексей Р
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.