Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to check each value in a pandas column contains a specific value?

I want to check if a column that contains a list of dictionaries contains a specific key.

In [128]: das.properties
Out[128]: 
0        [{'aon_uuid': 'ab8acae5fe37448589094f40...
1        [{'aon_uuid': '3e8e218a695240af823ea459...
2        [{'aon_uuid': 'd82085052cac4e888a79046f...
3        [{'aon_uuid': 'f4af2f4e085944a8b2cfa8ba...
4        [{'aon_uuid': '306c5229ebdb4abb8242bd33...
                               ...                        
35771    [{'aon_uuid': 'b472a9eac4f84ec1ad83db85...
35772    [{'aon_uuid': '237473f54ad94759a7914fbe...
35773    [{'aon_uuid': 'b79e683015374126b8f1a796...
35774    [{'aon_uuid': 'c0e08e43b8b84c40848e84ee...
35775    [{'aon_uuid': '452256b4fde744bf820100eb...

How can I assert that each column contains a aon_uuid? I want to assert if any of these rows in the properties column does not contain that uuid.

I have tried using isin but i don’t think it worked with a dictionary as values 🙁

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

from collections import ChainMap

df.properties.map(lambda x: ChainMap(*x)).str.contains('aon_uuid', regex=False)

ChainMap will join list of dictionaries, .str.contains then checks if the key is in the resulting dict.

Alternative: do it explicitly

df.properties.map(lambda l: any('aon_uuid' in d for d in l))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading