ByteBoard Notes Notes

Create a Series of monthly sales, increase every value by 10% and slice the last quarter

Informatics Practices XII · Experiment 06 of 38 · Pandas

12-IPPandas programs
Expt. No.06
Page No.12

Monthly sales: raise 10%, slice a quarter

Aim

To write a Python program that stores monthly sales in a Series, increases every value by 10% and displays the sales of the last quarter (October to December).

Concepts used

Algorithm

  1. Create a Series of 12 monthly sales with the month names as the index.
  2. Multiply the Series by 1.10 to raise every value by 10%.
  3. Slice the result from "Oct" to "Dec" and display it.

Program

# Monthly sales: raise every value by 10% and show the last quarter import pandas as pd months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",          "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]sales = pd.Series([42000, 38500, 45200, 51000, 47800, 53900,                   49500, 46100, 52300, 58700, 61200, 64800], index=months) new_sales = sales * 1.10print("Sales after a 10% rise:")print(new_sales)print("Last quarter (Oct to Dec):")print(new_sales["Oct":"Dec"])

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

Output

$ python series_monthly_sales.py
Sales after a 10% rise: Jan 46200.0 Feb 42350.0 Mar 49720.0 Apr 56100.0 May 52580.0 Jun 59290.0 Jul 54450.0 Aug 50710.0 Sep 57530.0 Oct 64570.0 Nov 67320.0 Dec 71280.0 dtype: float64 Last quarter (Oct to Dec): Oct 64570.0 Nov 67320.0 Dec 71280.0 dtype: float64

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

Result

Every month's sales were raised by 10% in a single operation (e.g. Jan 42000 → 46200), with no loop. The label slice "Oct":"Dec" displayed the last quarter, including December.

Viva voce: what the examiner may ask

01How was every value raised without a loop?

Arithmetic on a Series applies to every element at once (vector operation).

02Why did the dtype become float64?

Multiplying by 1.10 gives decimal results.

03How would you show the first quarter?

new_sales["Jan":"Mar"] or new_sales.head(3).

Variations for the lab test

  1. Display the months whose new sales are above 60000.
  2. Show the sales of the second quarter (Apr to Jun).
  3. Decrease the December sales by 5000 and display the Series.
! Common mistake Writing sales + 10% is a syntax error; a 10% rise is sales * 1.10 (or sales + sales * 10 / 100).
Back to the 12-IP index