Hello Data Scientists,

Disclaimer: I am a Beginner learning Python and at the same time just trying out a lot of things.

I am doing some data extraction on a file with with weekly deaths in Switzerland between the years of 2010 - 2019 (to be found at https://opendata.swiss/en/). I narrowed down the age group of 0-64, now I am separating this group into deaths per year.

enter image description here

I find it cumbersome to repeat assigning the variable to an operator. I wondered if there would be any way to define a function and input a list. Maybe it would be even nicer to have some operation extract the years from the file itself and work it out automatically.

Here is my state of knowledge: enter image description here

I tried to follow a template and as you can see I get a lot of error messages. Probably I am doing it all wrong?

Please let me know if you have any suggestions and how solve it and thank you very much for taking the time to ponder my question! :)


If you want to get the total number of deaths for each year in the data there is a much easier way than manually going through all the years and subsetting and summing the data. pandas has the groupby and agg methods for this:

file.groupby("KJ").agg({"Anzahl_Todesfalle": "sum"})

This line of code makes groups in the dataset based on the KJ column, and within those groups aggregates the Anzahl_Todesfalle column by summing all the values, returning one row for each year containing the total number of deaths.


Not the answer you're looking for? Browse other questions tagged or ask your own question.