Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用条件语句创建包含计算值的新列_Python_Python 3.x_Pandas_If Statement - Fatal编程技术网

Python 如何使用条件语句创建包含计算值的新列

Python 如何使用条件语句创建包含计算值的新列,python,python-3.x,pandas,if-statement,Python,Python 3.x,Pandas,If Statement,背景: 我有一个关于足球比赛统计的数据框架 此数据帧缺少有关匹配结果的信息 因此,我试图创建一个专栏,让我知道比赛结果是“赢”还是“输” 数据框包含主客场球队的进球 当主场进球多于客场进球时,我试图用“赢”来填充一个新的专栏 您可以在下面的代码中看到,我试图创建的新列名为“output” 下面的工作代码应该给我一个感觉,我正在尝试做什么,我做错了 for index, row in match_df.iterrows(): if match_df.home_team_goal >

背景:

  • 我有一个关于足球比赛统计的数据框架
  • 此数据帧缺少有关匹配结果的信息
  • 因此,我试图创建一个专栏,让我知道比赛结果是“赢”还是“输”
  • 数据框包含主客场球队的进球
  • 当主场进球多于客场进球时,我试图用“赢”来填充一个新的专栏
  • 您可以在下面的代码中看到,我试图创建的新列名为“output”

  • 下面的工作代码应该给我一个感觉,我正在尝试做什么,我做错了

    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:序列的真值不明确。使用a.empty、a.bool()、a.item()、a.any()或a.all()。
    当您使用
    熊猫时,应尝试不使用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')
    

    使用
    pandas
    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'
    

    与预期一样。

    您也可以使用以下选项:

    match_df['outcome'] = 'Win'
    match_df.loc[match_df.home_team_goal < match_df.away_team_goal, 'outcome'] = 'Lose'
    
    将如预期的那样。

    要编辑您的尝试:

    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") )
    
    要编辑您的文件,请尝试:

    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") )
    

    谢谢,这有帮助,对我来说也是新的。谢谢,这有帮助,对我来说也是新的。