1

In an Excel sheet with columns Rainfall / Year / Month, I want to sum rainfall data per year. That is, for instance, for the year 2000, from month 1 to 12, summing all the Rainfall cells into a new one. I tried using pandas in Python but cannot manage (just started coding). How can I proceed? Any help is welcome, thanks!

Here the head of the data (which has been downloaded):

   rainfall (mm)  \tyear   month  country   iso3   iso2
0      120.54000    1990       1      ECU    NaN    NaN
1      231.15652    1990       2      ECU    NaN    NaN
2      136.62088    1990       3      ECU    NaN    NaN
3      203.47653    1990       4      ECU    NaN    NaN
4      164.20956    1990       5      ECU    NaN    NaN

2 Answers 2

1

Use groupby and aggregate sum if need sum of all years:

df = df.groupby('\tyear')['rainfall (mm)'].sum()

But if need only one value:

df.loc[df['\tyear'] == 2000, 'rainfall (mm)'].sum()
Sign up to request clarification or add additional context in comments.

Comments

1

If you just want the year 2000, use

df[df['\tyear'] == 2000]['rainfall (mm)'].sum()

Otherwise, jezrael's answer is nice because it sums rainfall (mm) for each distinct value of \tyear.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.