week of the year pandas 1

week of the year pandas

from datetime import date
df_date = pd.DataFrame([date.today()],columns  = ['today'])
print(df_date)
#### Print Output ####
#        today
#0  2019-09-07
df_date['weeknum'] = df_date.today.apply(lambda x:x.isocalendar()[1])
print(df_date)
#### Print Output ####
#        today  weeknum
#0  2019-09-07       36

Here is what the above code is Doing:
1. Create a dataframe with a single column called ‘today’
2. Create a new column called ‘weeknum’
3. Apply a lambda function to the ‘today’ column to get the week number

Similar Posts