我想做以下几点:
下面是我对代码的尝试。
def play(df):
if df['Team'] in list(['Blue']):
return 'Exclude','**************'
df['Can he play?'],df['Why?'] = df.apply(play, axis = 1)我不知道如何从条件语句返回dataframe值。
如何返回“Blue”(df‘’Team‘中的值)

发布于 2018-11-08 23:05:10
这可以分为两个步骤,如下所示:
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 Nonehttps://stackoverflow.com/questions/53217388
复制相似问题