Filter data frame using condition

I created a dataframe like this:

d = {"one":[1,2,3],"two":[[["c",3],["a",4]],["b",5],["a",6]]}
pd.DataFrame(d)

      

My question is how to filter a new dataframe, column "two" only contains "a"

Expected result

+3


source to share


2 answers


This should do it for you:



df.loc[df["two"].astype(str).str.contains("a")] 

      

+2


source


You can use the method apply

to return a boolean mask that can be used to select the correct indices. The idea is to define the function that you want to act on each element Series

and apply it (using apply

) the whole series.



def fun(element):
    print(element)
    if type(element[0])==list:
        l=[e[0]=='a' for e in element]
    else:
        l = [element[0]=='a']

    if True in l:
        return True
    else:
        return False

df[df.two.apply(fun)]

      

0


source







All Articles