根据现有列的值创建新列

时间:2018-05-16 08:03:05

标签: python python-3.x pandas numpy series

我想创建一个新列(即Winning_Time),如下表所示。 Match_state(Winning)中的所有Time_diff将存储在新列 Winning_Time 中。其余行将填充NaN或零。 我怎么能这样做?

gsm_id  Goal_Flag   Union_Level Team_SR Match_state   Time_diff           Wining_Time
2462796 First goal  Scored      Burnley  Winning    0 days 00:23:15.00   0 days 00:23:15.00
2462796 First goal  Conceded    Chelsea  Losing     0 days 00:23:15.00   NaN               
2462796 Other goals Scored      Burnley  Winning    0 days 00:15:20.00   0 days 00:15:20.00
2462796 Other goals Conceded    Chelsea  Losing     0 days 00:15:20.00   NaN
2462796 Other goals Scored      Burnley  Winning    0 days 00:03:34.00   0 days 00:03:34.00
2462796 Other goals Conceded    Chelsea  Losing     0 days 00:03:34.00   NaN
2462796 Other goals Scored      Chelsea  Losing     0 days 00:25:59.00   NaN
2462796 Other goals Conceded    Burnley  Winning    0 days 00:25:59.00   0 days 25:59.00
2462796 Last goal   Scored      Chelsea  Losing     0 days 00:19:11.00   NaN
2462796 Last goal   Conceded    Burnley  Winning    0 days 00:19:11.00   0 days 00:19:11.00
2462795 First goal  Scored      City     Winning    0 days 01:09:15.00   0 days 01:09:15.00 
2462795 First goal  Conceded    Brighton Losing     0 days 01:09:15.00   NaN
2462795 Last goal   Scored      City     Winning    0 days 00:05:21.00   0 days 00:05:21.00
2462795 Last goal   Conceded    Brighton Losing     0 days 00:05:21.00   NaN

非常感谢您的建议。

1 个答案:

答案 0 :(得分:2)

您可以使用numpy.where

df['Winning_Time'] = np.where(df['Match_state'] == 'Winning', df['Time_diff'], np.nan)

在这里,numpy.where的作用类似于vectorised if / else语句。