I am new to programming and python and would like to write the following piece of code as a function using the 'def' 'return' construction:
df.loc[df['DATE_INT'].shift(-1) - df['DATE_INT'] == 1, 'CONSECUTIVE_DAY'] = True
df.loc[(df['DATE_INT'].shift(-1) - df['DATE_INT'] == 1) | (df['DATE_INT'].shift(1) - df['DATE_INT'] == -1), 'CONSECUTIVE_DAY'] = True
My attempt returns invalid syntax:
def ConsecutiveID(df, column ='DATE_INT'):
return df.loc[df['DATE_INT'].shift(-1) - df['DATE_INT'] == 1, 'CONSECUTIVE_DAY'] = True
df.loc[(df['DATE_INT'].shift(-1) - df['DATE_INT'] == 1) | (df['DATE_INT'].shift(1) - df['DATE_INT'] == -1), 'CONSECUTIVE_DAY'] = True
My goal is to ultimately use my ConsecutiveID function as follows:
df.groupby(['COUNTY_GEOID_YEAR','TEMPBIN']).apply(ConsecutiveID)
I am applying the split-apply-combine construction. Where groupby is splitting my data and I use the function I would like to construct in apply.
My main question is how to write what I've called the ConsecutiveID as a function. Thank you for any help.