passinger
passinger

Reputation: 431

How to filter a pandas dataframe by dict column?

Given a dataframe df['serialnumber', 'basicinfo'], the column "basicinfo" is a dict {'name': xxx, 'model': xxx, 'studyid': xxx}.

Is there an easy way to filter this dataframe by the dict key "model"?

We can do this filtered by 'serialnumber' if it is integer:

df = df[df.serialnumber == <value>]

How to do that for dict column?

Upvotes: 4

Views: 3900

Answers (1)

CodeMonkey
CodeMonkey

Reputation: 4738

You won't get the vectorized operations directly. But you could use apply to get the dictionary value from.

df = df[df.basicinfo.apply(lambda x: x['model'] == <value>)]

Upvotes: 9

Related Questions