我正在尝试执行以下操作:
下面是我尝试输入的代码。
def play(df):
if df['Team'] in list(['Blue']):
return 'Exclude','**************'
df['Can he play?'],df['Why?'] = df.apply(play, axis = 1)
我不知道如何从条件语句返回数据框值
如何返回'Blue'(df ['Team']中的值)
答案 0 :(得分:1)
这可以分两步完成,就像这样:
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