Artificial Intelligence / Python Libraries / NumPy

Analyze India rainfall from a CSV

Download a small real dataset, load its columns into NumPy, and explore annual and monsoon rainfall.

1. Understand the data

This lesson uses rainfall data for all of India. Each row in the CSV represents one year. The original dataset was prepared from India Meteorological Department data and published on the Government of India’s Open Government Data portal. It contains monthly, seasonal, and annual rainfall from 1901 to 2015.

To keep our first analysis small and easier to follow, the downloadable sample includes just 10 years and three columns:

Columns in the sample CSV
ColumnMeaningExample
yearThe calendar year for this row.2006
annual_rainfall_mmAll-India area-weighted rainfall for the whole year, in millimeters.1199.4 mm in 2006
monsoon_jun_sep_mmAll-India area-weighted rainfall during June, July, August, and September, in millimeters.927.8 mm in 2006

Area-weighted means the rainfall estimates are combined to represent the country as a whole, accounting for the areas represented. This is not rainfall for one city or one rain gauge. The monsoon column is already the June–September total, so we don’t need to add the four months ourselves.

Here is what the first row says in plain language: in 2006, the all-India annual rainfall was 1199.4 mm, and 927.8 mm of it fell during June through September.

Open the official India rainfall dataset catalog · Open the full 1901–2015 dataset resource

2. Download and load the CSV

First download the small sample file. Then choose that file below. The code runner will copy the selected CSV into its temporary browser workspace, where NumPy can read it. The file stays on your device.

Download India rainfall sample (CSV) ↓

Choose india-rainfall-sample-2006-2015.csv. Select it once; you can then run the examples below.

The first row of the CSV contains column names, so skiprows=1 tells NumPy to skip that header. The remaining rows contain only numbers, which np.loadtxt() can read into a 2D array.

Choose the downloaded CSV, then run the code.
Output will appear here.

In data[:, 1], the colon means “all rows” and 1 selects the second column. Python starts counting columns at 0, so column 0 is the year, column 1 is annual rainfall, and column 2 is monsoon rainfall.

3. Find the wettest and driest years

Now use mean() to calculate the average annual rainfall across these 10 years. argmax() and argmin() find the positions of the highest and lowest annual totals; we use those positions to look up the matching years.

Ready to run in your browser.
Output will appear here.

The answers describe only the 10 years in our sample. The full government dataset covers a much longer period.

4. How much of the yearly rain falls during monsoon?

For each year, divide June–September rainfall by annual rainfall and multiply by 100. This gives the monsoon share of that year’s total as a percentage.

Ready to run in your browser.
Output will appear here.

This percentage helps describe how rainfall is distributed through the year in this national dataset. It doesn’t mean every location in India has the same rainfall pattern.

What we learned

We started with a downloadable CSV, loaded its numeric values with np.loadtxt(), selected columns with slicing, and used NumPy calculations to summarize real rainfall data. The rows are years; the columns are measurements; each calculation answers a question about those measurements.

For more examples, return to the NumPy guide or read Advanced NumPy.