Munichong
Munichong

Reputation: 4041

Pandas: Select rows whose dictionary contains a specific key

I have a dataframe, in which one column is all dictionary. I want to select rows whose dictionary contains a given key.

>>> df = pd.DataFrame({"A": [1,2,3], "B": [{"a":1}, {"b":2}, {"c":3}]})
>>> df
   A         B
0  1  {'a': 1}
1  2  {'b': 2}
2  3  {'c': 3}
>>> df['b' in df['B']]  
# the desired result is the row with index 1. But this causes an error: KeyError: False

Upvotes: 4

Views: 3621

Answers (2)

BENY
BENY

Reputation: 323326

Using get the dict keys

df.B.apply(lambda x : 'b' in x.keys())
Out[89]: 
0    False
1     True
2    False
Name: B, dtype: bool

Upvotes: 0

jpp
jpp

Reputation: 164773

Here is one way:

df = pd.DataFrame({"A": [1,2,3], "B": [{"a":1}, {"b":2}, {"c":3}]})

df = df[df['B'].map(lambda x: 'b' in x)]

#    A         B
# 1  2  {'b': 2}

Explanation

  • pd.Series.map accepts anonymous (lambda) functions as an argument.
  • The function takes each element of B and checks whether b is in that element, returning a Boolean series.
  • We use the natural Boolean indexing of df[bool_series] to choose the required rows.

Upvotes: 8

Related Questions