0

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.

1 Answer 1

2

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.

1
  • 1
    i just happened to find out. Anyway. Thanks a lot @jpp
    – Zephyr
    Commented May 16, 2018 at 8:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.