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