Home > OS >  Pandas: Filter a column by the value from applying a function to another column?
Pandas: Filter a column by the value from applying a function to another column?

Time:03-22

Suppose I have a df with 2 columns A and B and a class Foo() which can be instantiated by A's value. I'd like to get a unique series of B which satisfy Foo(A).item.price == 0. I tried the following code which obviously doesn't work.

unique_B = df.loc[Foo(df.A.str).item.price == 0, "B"].unique()

I guess I could create a third column to store the value of foo(df.A), is there a simpler solution?

CodePudding user response:

Use df.A.apply(foo) instead of foo(df.A.str):

unique_B = df.loc[df.A.apply(foo) == 0, "B"].unique()

To access the item property of the foo class:

unique_B = df.loc[df.A.apply(lambda x: foo(x).item.price) == 0, "B"].unique()
  • Related