简体   繁体   中英

Pandas filter dataframe by values in a list of dictionaries

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. ?

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': '...

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': '...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM