Reputation: 1352
I would like to create a new column (I.e. Winning_Time) as shown below table. All the Time_diff at Match_state ( Winning) will be stored in the new column Winning_Time. The rest rows will be filled with NaN or zero. 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.
Upvotes: 0
Views: 62
Reputation: 164623
You can use 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.
Upvotes: 2