[英]Creating a new column based on value of existing column
I would like to create a new column (Ie Winning_Time) as shown below table. 我想创建一个新列(即Winning_Time),如下表所示。 All the Time_diff at Match_state ( Winning) will be stored in the new column Winning_Time . Match_state(Winning)处的所有Time_diff将存储在新列Winning_Time中 。 The rest rows will be filled with NaN or zero. 其余行将用NaN或零填充。 How can I do that? 我怎样才能做到这一点?
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
Your advice is much appreciated. 非常感谢您的建议。
You can use numpy.where
: 您可以使用numpy.where
:
df['Winning_Time'] = np.where(df['Match_state'] == 'Winning', df['Time_diff'], np.nan)
Here, numpy.where
acts like a vectorised if / else statement. 在这里, numpy.where
作用类似于向量化if / else语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.