I'm trying to populate the 'Selected' column with the values found in the 'Name' column if the conditions for 'Name' and 'Age' are true. Else, remain column as empty string. However, the program seems to not reading the if condition as it jumps to the result inside 'else'.
import pandas as pd
data = {'Name': ['Tom', 'Joseph', 'Krish', 'John'],
'Age': ['20', '35', '43', '50'],
'Selected': ' '}
df = pd.DataFrame(data)
df['Selected'] = df['Selected'].apply(lambda x: df['Name'] if ((df['Name']).any() == 'Tom') &
(df['Age'].any() < 25) else ' ')
print(df)
Here's the output of above code:
Name Age Selected
0 Tom 20
1 Joseph 35
2 Krish 43
3 John 50
whereas I'm expecting to see Tom in the Selected column at the same index for the row because Tom has met the conditions for both 'Name' and 'Age'. --> Tom < 25
Any helps are appreciated, thanks!