0

I am trying to do the following:

  1. Pass Dataframe[['ID']['Team'] to function
  2. If Team is Blue, return 'Y' to 'Can he play?' and also return the value 'Blue' to 'Why?'

Below is my attempt at the code.

def play(df):
if df['Team'] in list(['Blue']):
    return 'Exclude','**************'  

df['Can he play?'],df['Why?'] = df.apply(play, axis = 1)

I do not know how to return the dataframe value from the conditional statement

How do I return 'Blue' (the value in df['Team'])

enter image description here

2
  • Hi there. When posting Python code please take care to make sure your indentation is correctly formatted. You can copy/paste your exact code into the edit form, select it, and press Ctrl-K to indent it all at once. Commented Nov 8, 2018 at 23:05
  • I'm really confused why you wrote if df["Team"] in list["Blue"]). First of all ["Blue"] is already a (single element) list, so wrapping that in list() just makes a useless copy of it. But second, why not just if df["Team"] == "Blue"? Commented Nov 8, 2018 at 23:10

1 Answer 1

1

This can be done in two steps, like so:

df = pd.DataFrame({'Team': ['Blue', 'Green', 'Blue', 'Red']})
colour = 'Blue'
df['Can he play?'] = np.where(df['Team'] == colour, 'Y', None)
df['Why?'] = np.where(df['Team'] == colour, colour, None)

    Team Can he play?  Why?
0   Blue            Y  Blue
1  Green         None  None
2   Blue            Y  Blue
3    Red         None  None
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.