My df looks like the following
| col1 | col_x |
|---|---|
| ... | {"key_x":"value1"} |
| ... | None |
| ... | {"key_x":"value2"} |
How to select all items with {"key_x":"value1"} in col_x ?
col_x values could be dict or None.
What I've tried:
df.loc[df['col_x']['key_x'] == 'value1'] # KeyError: 'key_x'
df[~df['col_x'].isnull()].loc[df['col_x']['key_x'] == 'value1'] # KeyError: 'key_x'
df_ok = df[[isinstance(x, dict) and ('key_x' in x.keys()) for x in df.col_x]]
df_ok.loc[df_ok['col_x']['key_x'] == 'value1'] # KeyError: 'key_x'
(last one syntax according this answer)
Lets us use
str.getto yank the value from dictionary. Thestr.getmethod can handle null values as well as missing keys.