0

I started with the this post: Pandas conditional creation of a series/dataframe column

In my specific case my data look like this:

    Type       Set
1    A        [1,2,3]
2    B        [1,2,3]         
3    B        [3,2,1]
4    C        [2,4,1]

I borrowed the idea using np.where, so if I need to create a new col based on the last element from the list in each entry, I wrote:

df['color'] = np.where(df['Set'].str(-1)==3, 'green', 'red'), and this yields:

  Set Type  color
0   Z [1,2,3]  green
1   Z [1,2,3]  green
2   X [3,2,1]    red
3   Y [2,4,1]    red

Now I wish to be more flexible, want to say, if 3 shows in the list at all, I will assign color=green. I tried using in or isin(), they don't work with np.where. Wish to learn what other options in a similar format I have above. (not using for loop if possible).

The desired output:
      Set Type  color
    0   Z [1,2,3]  green
    1   Z [1,2,3]  green
    2   X [3,2,1]  green
    3   Y [2,4,1]    red

2 Answers 2

4

Try with explode

df['new']= np.where(df.Set.explode().eq(3).any(level=0),'green','red')
df
Out[131]: 
        Type    new
0  [1, 2, 3]  green
1  [1, 2, 3]  green
2  [3, 2, 1]  green
3  [2, 4, 1]    red
1
  • I think that should be df.Set and not df.Type. Feel free to revert if I am incorrect. Commented May 20, 2021 at 14:59
2

You can also convert to string and use str.contains:

find=3
df['color'] = np.where(df["Set"].astype(str).str.contains(str(find)),'green','red')

Or with a dataframe where the condition will be

pd.DataFrame(df["Set"].to_list(),index=df.index).eq(3).any(1)

print(df)

  Type        Set  color
1    A  [1, 2, 3]  green
2    B  [1, 2, 3]  green
3    B  [3, 2, 1]  green
4    C  [2, 4, 1]    red

Not the answer you're looking for? Browse other questions tagged or ask your own question.