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 :(
CodePudding user response:
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))
To check if all values have the key, just add .all()
at the end:
df.properties.map(lambda l: any('aon_uuid' in d for d in l)).all()