ByteBoard Notes Notes

Take data from an open source such as data.gov.in, summarise it and plot it

Informatics Practices XII · Experiment 22 of 38 · Pandas

★ CBSE suggested12-IPMatplotlib programs
Expt. No.22
Page No.44

Open data: rainfall summary

Aim

To write a Python program that reads a CSV file in the format of a data.gov.in dataset (state-wise monsoon rainfall), adds a total column to summarise it, and plots it with two kinds of chart. (The values in this sample file are for practice; download a real dataset from data.gov.in for your project.)

Concepts used

Algorithm

  1. Read rainfall.csv into a DataFrame.
  2. Add a Total column: Jun + Jul + Aug + Sep.
  3. Display the DataFrame and the states with more than 1000 mm in total.
  4. Draw a bar chart of the totals.
  5. Draw a line chart of each state's monthly rainfall, looping with iterrows(), with a legend.

Program

# Summarising and plotting rainfall data import pandas as pdimport matplotlib.pyplot as plt df = pd.read_csv("rainfall.csv")df["Total"] = df["Jun"] + df["Jul"] + df["Aug"] + df["Sep"]print(df)print("States with more than 1000 mm:")print(df[df["Total"] > 1000]) plt.bar(df["State"], df["Total"], color="steelblue")plt.xlabel("State")plt.ylabel("Rainfall (mm)")plt.title("Total monsoon rainfall, June to September")plt.show() months = ["Jun", "Jul", "Aug", "Sep"]for label, row in df.iterrows():    plt.plot(months, [row["Jun"], row["Jul"], row["Aug"], row["Sep"]], label=row["State"])plt.xlabel("Month")plt.ylabel("Rainfall (mm)")plt.title("Monthly rainfall by state")plt.legend()plt.show()

A marked line continues the line above it. In your file, write it on the same line.

Input file

rainfall.csv6 lines
State,Jun,Jul,Aug,Sep
Kerala,650,720,480,250
Maharashtra,210,340,290,180
Rajasthan,60,170,150,70
Assam,420,390,330,250
Punjab,70,190,160,80

Output

$ python plot_open_data.py
State Jun Jul Aug Sep Total 0 Kerala 650 720 480 250 2100 1 Maharashtra 210 340 290 180 1020 2 Rajasthan 60 170 150 70 450 3 Assam 420 390 330 250 1390 4 Punjab 70 190 160 80 500 States with more than 1000 mm: State Jun Jul Aug Sep Total 0 Kerala 650 720 480 250 2100 1 Maharashtra 210 340 290 180 1020 3 Assam 420 390 330 250 1390
Figure 1
Bar chart of total monsoon rainfall by state: Kerala 2100 mm, Maharashtra 1020, Rajasthan 450, Assam 1390, Punjab 500.
Figure 2
Line chart of monthly rainfall from June to September for five states; Kerala is highest throughout, and most states peak in July.

✓ Printed by running this program, not typed by hand.

Result

Kerala (2100 mm), Assam (1390 mm) and Maharashtra (1020 mm) had more than 1000 mm in total. The bar chart compares the totals, and the line chart shows that rainfall peaked in July in every state except Assam, where June was the wettest month.

Viva voce: what the examiner may ask

01What is data.gov.in?

The Government of India's open data portal, where datasets can be downloaded, often as CSV files.

02Why was the Total column added before plotting?

To summarise four months into one value per state for the bar chart.

03Why does each state get its own line in the second chart?

plt.plot() is called once per row inside the loop, each with the state's name as the label.

Variations for the lab test

  1. Download a real dataset from data.gov.in and repeat the steps.
  2. Plot only the states with a total above 800 mm.
  3. Add an Average column (Total ÷ 4) and plot it.
! Common mistake Using a downloaded file without opening it first: real datasets often have extra header lines or different column names, so check the file before calling read_csv().
Back to the 12-IP index