Reputation: 4766
I have the following structure in my data frame (mydata)
name values
abc [{"x":"pqr","y":"lrz"}, {"x":"wer", "y":"rty"}]
bcd [{"x":"pqs","y":"eer"}, {"x":"pqr", "y":"dww"}]
It has two columns. The values column has a list of dictionaries. I want to filter the dataframe where the first element of the values list has "x"= "pqr" value.
Expected dataframe
name values
abc [{"x":"pqr","y":"lrz"}, {"x":"wer", "y":"rty"}]
I tried
mydata[mydata["values"][0]["x"] == "pqr"]
but I got keyerror 0. Is there any way to do this without iterating through the dataframe. ?
Upvotes: 1
Views: 602
Reputation: 19947
You can also use an apply:
df[df['values'].apply(lambda x: x[0]['x']=='pqr')]
name values
0 abc [{'x': 'pqr', 'y': 'lrz'}, {'x': 'wer', 'y': '...
Upvotes: 2
Reputation: 29742
Use pandas.Series.str
:
df[df['values'].str[0].str['x'].eq("pqr")]
Output:
name values
0 abc [{'x': 'pqr', 'y': 'lrz'}, {'x': 'wer', 'y': '...
Upvotes: 5