Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create new pandas column with apply based on conditions of multiple other columns

I have a df with time stamps in a basketball game (this is a sample. my actual df is much larger)

    year    period  p_sec_rem
0   2015    1   556
1   2013    1   455
2   2001    2   67
3   2008    3   177
4   2017    1   172

period is the period of the game and p_sec_remaining is the seconds remaining in the period. I would like to calculate how many seconds have elapsed since the beginning of the game (time) and have some logic that does this. It is a bit complicated, but explaining it is besides the point as I am sure it is correct:

df['time'] = None

def secondsPlayed(df):
  if df.year >= 2006:
    if df.period == 1:
      df['time'] = 600 - df.p_sec_rem
    elif df.period > 1 & df.period < 5:
      df['time'] = ((df.period * 10) * 60) - df.p_sec_rem
    elif df.period == 5:
      df['time'] = (((4 * 10) * 60) + 300) - df.p_sec_rem
    elif df.period == 6:
      df['time'] = (((4 * 10) * 60) + 600) - df.p_sec_rem
    elif df.period == 7:
      df['time'] = (((4 * 10) * 60) + 900) - df.p_sec_rem
  elif df.year <= 2005:
    if df.period == 1:
      df['time'] = 1200 - df.p_sec_rem
    elif df.period == 2:
      df['time'] = 2400 - df.p_sec_rem
    elif df.period == 3:
      df['time'] = (((2 * 20) * 60) + 300) - df.p_sec_rem
    elif df.period == 4:
      df['time'] = (((2 * 20) * 60) + 600) - df.p_sec_rem
    elif df.period == 5:
      df['time'] = (((2 * 20) * 60) + 900) - df.p_sec_rem
    elif df.period == 6:
      df['time'] = (((2 * 20) * 60) + 1200) - df.p_sec_rem
    elif df.period == 7:
      df['time'] = (((2 * 20) * 60) + 1500) - df.p_sec_rem

I would like to apply this logic to each row of my df, so I figured using *.apply() would do the trick, but alas:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df.apply(secondsPlayed,axis=1)

0    None
1    None
2    None
3    None
4    None

All None are returned, when my desired output is:

0    44
1    145
2    2333
3    1623
4    428

I came across this question but I think it is slightly different from my example since I need to pass an entire df to the function as I utilize multiple different columns for the conditions that determine the output df.time value. I suspect I am close, but have been playing around with this for a while to no avail.

>Solution :

You just need to return the df in your function:

def secondsPlayed(df):
    if df.year >= 2006:
        if df.period == 1:
            df['time'] = 600 - df.p_sec_rem
        elif df.period > 1 & df.period < 5:
            df['time'] = ((df.period * 10) * 60) - df.p_sec_rem
        elif df.period == 5:
            df['time'] = (((4 * 10) * 60) + 300) - df.p_sec_rem
        elif df.period == 6:
            df['time'] = (((4 * 10) * 60) + 600) - df.p_sec_rem
        elif df.period == 7:
            df['time'] = (((4 * 10) * 60) + 900) - df.p_sec_rem
    elif df.year <= 2005:
        if df.period == 1:
            df['time'] = 1200 - df.p_sec_rem
        elif df.period == 2:
            df['time'] = 2400 - df.p_sec_rem
        elif df.period == 3:
            df['time'] = (((2 * 20) * 60) + 300) - df.p_sec_rem
        elif df.period == 4:
            df['time'] = (((2 * 20) * 60) + 600) - df.p_sec_rem
        elif df.period == 5:
            df['time'] = (((2 * 20) * 60) + 900) - df.p_sec_rem
        elif df.period == 6:
            df['time'] = (((2 * 20) * 60) + 1200) - df.p_sec_rem
        elif df.period == 7:
            df['time'] = (((2 * 20) * 60) + 1500) - df.p_sec_rem
    return df

output:

    year    period  p_sec_rem   time
0   2015         1      556     44
1   2013         1      455     145
2   2001         2       67     2333
3   2008         3      177     1623
4   2017         1      172     428
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading