I have a pandas dataframe that I would prefer to use a lambda function rather than a loop to solve my problem.
The problem is as such;
df = pd.DataFrame({'my_fruits':['fruit', 'fruit', 'fruit', 'fruit', 'fruit'],
'fruit_a': ['apple', 'banana', 'vegetable', 'vegetable', 'cherry'],
'fruit_b': ['vegetable', 'apple', 'vegeatble', 'pineapple', 'pear']})
If I apply the following loop;
for i in np.arange(0,len(df)):
if df['fruit_a'][i] == 'vegetable' or df['fruit_b'][i] == 'vegetable':
df['my_fruits'][i] = 'not_fruit'
I am able to get the result that I want. This is that if either of the fruit_a or fruit_b columns containing the value vegetable, I want the my_fruits column to be equal to not_fruit.
How can I possible set this up in a lamda function. Was not able to understand how two columns inputs can be used to change a different columns values. Thanks!
return <expression>. A function is not an alternative for aforloop. The alternative to certain special cases of for loop is a comprehension, but your loop is not such a special case.