I have dates for each row in my dataframe and want to assign a value to a new column based on a condition of the date.
Normally if I assign a value to a new column, I would do something like this:
def get_mean(df):
return df.assign(
grouped_mean = lambda df: df.groupby('group')['X']
.transform(lambda df: df.mean())
)
No I am looking for a solution like that, since the solution I have now is very slow and not beautiful.
Is there a better way than my current solution and use assign?
I currently came up with this solution:
def set_season(df):
df = df.copy()
for i in df.index:
if (df.loc[i, 'Date'] >= pd.Timestamp('2008-08-30')) & (df.loc[i, 'Date'] <= pd.Timestamp('2009-05-31')):
df.at[i, 'season'] = '08-09'
elif (df.loc[i, 'Date'] >= pd.Timestamp('2009-08-22')) & (df.loc[i, 'Date'] <= pd.Timestamp('2010-05-16')):
df.at[i, 'season'] = '09-10'
elif (df.loc[i, 'Date'] >= pd.Timestamp('2010-08-28')) & (df.loc[i, 'Date'] <= pd.Timestamp('2011-05-22')):
df.at[i, 'season'] = '10-11'
return df