1

Similar problem to this (How do I sum values in a column that match a given condition using pandas?) but in my case (https://i.stack.imgur.com/kEx1c.jpg) I have the date format in YYYY-MM-DD where I want to summarize all values in column 4 for every year. (The task is to create a function 'Rain_year' which when given an input year gives you the total rain that year)

Code:

def rain_year(a):
    df.loc['a-01-01':'a-12-31']
    df.loc[df['Dygn'] ==a, 'Nederbörd(mm)'].sum()
    return row["Nederbörd(mm)"].sum()
rain_year("1995")
print(rain_year("1995"))

But this doesn't give me anything (neither a value or an error message).

Kovac
  • 21
  • 1
  • 2
    1) Post your dataset! See [here](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) for why. 2) You probably want to [group by year](https://stackoverflow.com/questions/11391969/how-to-group-pandas-dataframe-entries-by-date-in-a-non-unique-column) and sum. – Nick ODell Sep 20 '21 at 17:35

1 Answers1

0

You could filter your data on the year you want and then sum the new group like so:

def rain_year(df,year):
    year_hold = df[df["date column"].dt.strftime("%Y")==year]
    return year_hold["Nederbörd(mm)"].sum()

print(rain_year(df,"1995"))

this should return something similar to what you were wanting assuming your date column is a date time. If not you would need to convert that column to a datetime like so.

df["date column"] = pd.to_datetime(df["date column"])
bellerb
  • 137
  • 8