Home > OS >  How to get each values of a column in a data frame that contains a list of dictionaries?
How to get each values of a column in a data frame that contains a list of dictionaries?

Time:04-02

As you can see I have a column that contains an array of dictionaries. I need to see if a key in any item has a certain value and return the row if it does.


0         [{'id': 473172988, 'node_id': 'MDU6TGFiZWw0NzM...
1         [{'id': 473172988, 'node_id': 'MDU6TGFiZWw0NzM...
2         [{'id': 473172988, 'node_id': 'MDU6TGFiZWw0NzM...
3         [{'id': 473173351, 'node_id': 'MDU6TGFiZWw0NzM...

Is there a straightforward approach for this? The datatype of the column is an object.

CodePudding user response:

You would need to give the exact format of your dictionary, but on the general principle you should loop over the elements:

key = 'xxx'
value = 'yyy'
out = [any(d.get(key) == value for d in l) for l in df['your_column']]

# slicing rows
df[out]
  • Related