Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter a pandas dataframe by dict column?

Tags:

python

pandas

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?

like image 488
passinger Avatar asked Sep 15 '25 13:09

passinger


1 Answers

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>)]
like image 86
CodeMonkey Avatar answered Sep 18 '25 09:09

CodeMonkey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!