I have a csv-file: https://data.rivm.nl/covid-19/COVID-19_aantallen_gemeente_per_dag.csv
I want to use it to provide insight into the corona deaths per week.
df = pd.read_csv("covid.csv", error_bad_lines=False, sep=";") df = df.loc[df['Deceased'] > 0] df["Date_of_publication"] = pd.to_datetime(df["Date_of_publication"]) df["Week"] = df["Date_of_publication"].dt.isocalendar().week df["Year"] = df["Date_of_publication"].dt.year df = df[["Week", "Year", "Municipality_name", "Deceased"]] df = df.groupby(by=["Week", "Year", "Municipality_name"]).agg({"Deceased" : "sum"}) df = df.sort_values(by=["Year", "Week"]) print(df)
Everything seems to be working fine except for the first 3 days of 2021. The first 3 days of 2021 are part of the last week (53) of 2020: http://week-number.net/calendar-with-week-numbers-2021.html.
When I print the dataframe this is the result:
53 2021 Winterswijk 1 Woudenberg 1 Zaanstad 1 Zeist 2 Zutphen 1
So basically what I'm looking for is a way where this line returns the year of the week number and not the year of the date:
df["Year"] = df["Date_of_publication"].dt.year