2

Here is an example Dataframe:

id  lists
1   ['dog', 'apple']
2   ['apple', 'cat']
3   ['pig', 'love']
4   ['help', 'out']

Now, I'd like to use lambda functions to create another column when apple is in a lists list.

id  lists             flag
1   ['dog', 'apple']  1
2   ['apple', 'cat']  1
3   ['pig', 'love']   0
4   ['help', 'out']   0

My first thought is to use the following code but I'm getting a syntax error:

df.apply(lambda x: [1 if "apple" in i for i in x])
madsthaks
  • 2,091
  • 6
  • 25
  • 46
  • 1
    What should the value be if apple isn't in i? – jonrsharpe Jan 22 '19 at 21:00
  • @jonrsharpe I'm guessing 0, from the MWE. Although that should be stated in the question explicitly. – cs95 Jan 22 '19 at 21:00
  • You are complicating LC, just use df['lists'].apply(lambda x: 'apple' in x).astype(int) – Vaishali Jan 22 '19 at 21:01
  • @coldspeed I meant more that that's the question the syntax error is asking. – jonrsharpe Jan 22 '19 at 21:01
  • I do not recommend the use of `apply` here. Use list comprehensions instead, if you are dealing with strings or mixed/mutable data types. See [this post](https://stackoverflow.com/questions/54028199/for-loops-with-pandas-when-should-i-care/54028200#54028200) for more information. – cs95 Jan 22 '19 at 21:08

1 Answers1

0

I think you can slightly modify your code and get the desired result as follows:

df["lists"].apply(lambda x: 1 if "apple" in x else 0)

this uses the ternary operator to return 1 if apple is in the list and 0 if it is not

Or as mentioned in the comments by @Vaishali you could use a simpler lambda and do a type conversion:

df["lists"].apply(lambda x: "apple" in x).astype(int)

The lambda here returns a boolean series which is then converted into 1s and 0s when casting as integer

Sven Harris
  • 2,884
  • 1
  • 10
  • 20