Count Rainy Days with Python

In this article, I will walk you through a numerical computational task with Python to count rainy days with python. Imagine you have a data series that represents the amount of precipitation each day for a year in a given city.

For this task of numerical computing with python to count rainy days within a year, I will start this off by loading the data, that you can download from here:

Also, Read – NLP Projects for Machine Learning.

import pandas as pd import numpy as np rainfall = pd.read_csv("Seattle2014.csv")["PRCP"].values inches = rainfall/254 print(inches.shape)
Code language: JavaScript (javascript)


The table contains 365 values, giving the daily precipitation in inches from January 1 to December 31, 2014.

Before we go ahead with this task, let’s take a quick look at the rainy day histogram using matplotlib:

import matplotlib.pyplot as plt import seaborn seaborn.set() plt.hist(inches, 40)
Code language: CSS (css)

The histogram above gives us a general idea of what the data looks like: despite its reputation, the vast majority of days in Seattle saw near the zero measured rainfall in 2014. 

Count Rainy Days with Numerical Computing

The focus of this article is about numerical computing with python to count rainy days within a year, so I will focus more on solving this problem by numerical computing rather than machine learning algorithms.

If you don’t know to perform the task of numerical computing in python we can easily perform our tasks using the NumPy package in Python. So let’s perform some necessary NumPy functions for numerical computing with Python.

Using the numerical calculation functions using NumPy, we could begin to answer the types of questions we receive about our precipitation data. Here are some examples of results we can calculate for counting rainy days using Python:

print("Number days without rain: ", np.sum(inches == 0)) print("Number days with rain: ", np.sum(inches != 0)) print("Days with more than 0.5 inches:", np.sum(inches > 0.5)) print("Rainy days with < 0.1 inches:", np.sum((inches > 0) & (inches < 0.2)))
Code language: PHP (php)

Number days without rain: 215
Number days with rain: 150
Days with more than 0.5 inches: 37
Rainy days with < 0.1 inches: 75

I hope you liked this article on how we can use the power of numerical computing in python to count the number of rainy days in Python. I hope you will play more with the data. Feel free to ask your valuable questions in the comments section below.

Also, Read – JSON in Python Tutorial.

Follow Us:

Default image
Aman Kharwal

I am a programmer from India, and I am here to guide you with Data Science, Machine Learning, Python, and C++ for free. I hope you will learn a lot in your journey towards Coding, Machine Learning and Artificial Intelligence with me.

Leave a Reply

Data Science | Machine Learning | Python | C++ | Coding | Programming | JavaScript