How to count nonzero occurrences based on another variable in python?

Viewed 47
Date      Precipitation
20010101  0
20010102  10
20010103  5
20010104  3
20010105  0
...
20011231  0

I have dataset showing precipitation (in) per each day in the year 2001. The date variable is in YYYYMMDD format. I want to calculate how many times it precipitated each month. In other words, I need the number of times the precipitation value is not 0 per each month.

I am a beginner python learner and don’t quite know how to tell the program to output the count per each month without having to do it individually.

The code I have below does not work because I’m not sure how to tell the program the Date variable is in YYYYMMDD format.

Precip_Count= Date[(Precipitation !=0)]

Is there a way to do this by only using NumPy?

1 Answers

First, convert Date column to datetime using pd.to_datetime and specify the format of your datetime string Datetime format code, then use Series.ne to find non-zero values, groupby month and take the sum using GroupBy.sum

df['Date'] = pd.to_datetime(df['Date'], format="%Y%M%d")
df['Precipitation'].ne(0).groupby(df.Date.dt.month).sum()

Date
1    3
...
12   0
Name: Precipitation, dtype: int64

OR using Series.dt.to_period here.

df['Precipitation'].ne(0).groupby(df.Date.dt.to_period('M')).sum()

Date
2001-01    3
...
2001-12    0
Freq: M, Name: Precipitation, dtype: int64

If you want index as DatetimeIndex use pd.Grouper

df['Precipitation'].ne(0).groupby(pd.Grouper(freq='M')).sum()

Date
2001-01-31    3
...
2001-12-31    0
Freq: M, Name: Precipitation, dtype: int64

The output is calculated from df mentioned in the question.

Related