
[英]Create multiple dataframe columns containing calculated values from an existing column
[英]How to create New Column containing Calculated Values with Conditional Statements in play
内容:
下面的工作代码应该可以让我了解我正在尝试做的事情,并且做错了...
for index, row in match_df.iterrows():
if match_df.home_team_goal > match_df.away_team_goal:
match_df.loc[index, "outcome"] = "Win"
else:
match_df.loc[index, "outcome"] = "Lose"
错误信息:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
使用pandas
时,您应该尝试不使用for循环: np.where
import pandas as pd; import numpy as np
match_df['outcome']=np.where(match_df.home_team_goal > match_df.away_team_goal,'win','lose')
您也可以使用以下内容:
match_df['outcome'] = 'Win'
match_df.loc[match_df.home_team_goal < match_df.away_team_goal, 'outcome'] = 'Lose'
现在:
print(match_df)
会如预期的那样。
编辑您的尝试:
for index, row in match_df.iterrows():
if row['home_team_goal'] > row['away_team_goal']:
match_df.loc[index, "outcome"] = "Win"
else:
match_df.loc[index, "outcome"] = "Lose"
或使用以下代码获得快速结果:
df.outcome.fillna(np.where(df.home_team_goal > df.away_team_goal, "Win", "Lose") )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.