Tag Archives: date frame

Python data analysis dataframe converts dates to weeks

When doing data analysis with Python, how to add a new column of weeks “week” and change the date, such as “2021-03-02” to the nth week of 2021?

Introduce two methods:
the dataframe of my data is called sample, which has the feature “date”. Now add a new feature “week”
1 dt.week

sample["week"]=sample["date"].dt.week

2. Use datetimeindex

df['week'] = pd.DatetimeIndex(sample['date']).week