Skip to content
Exercises · Q8

Q.Using the Series created in Question 5, write commands for the following:

a) Display the names of the months 3 through 7 from the Series MonthDays.
b) Display the Series MonthDays in reverse order.
CBSENCERTSubjective· 3mImportance★★★★★
42% · 16/38 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

This solution demonstrates how to perform common Pandas Series operations: slicing by label to extract a specific range of months and reversing the order of the entire Series.

When working with Pandas Series, especially in data analysis and manipulation tasks, two fundamental operations are slicing and reordering. Slicing allows you to select specific subsets of data, while reordering helps in presenting data in a different sequence, often for better readability or further processing.

First, let's establish the MonthDays Series, as it's referred to from "Question 5". We'll create a Series where the index represents month names and the values represent the number of days in those months for a non-leap year.

import pandas as pd

# Create the MonthDays Series
data = {
    'Jan': 31, 'Feb': 28, 'Mar': 31, 'Apr': 30,
    'May': 31, 'Jun': 30, 'Jul': 31, 'Aug': 31,
    'Sep': 30, 'Oct': 31, 'Nov': 30, 'Dec': 31
}
MonthDays = pd.Series(data)

print("Original Series MonthDays:")
print(MonthDays)
Original Series MonthDays:
Jan    31
Feb    28
Mar    31
Apr    30
May    31
Jun    30
Jul    31
Aug    31
Sep    30
Oct    31
Nov    30
Dec    31
dtype: int64

a) Display the names of the months 3 through 7 from the Series MonthDays.

To display the names of months 3 through 7, we need to identify which months these correspond to. Assuming a standard chronological order where January is the 1st month, February the 2nd, and so on:

  • 3rd month: March ('Mar')
  • 4th month: April ('Apr')
  • 5th month: May ('May')
  • 6th month: June ('Jun')
  • 7th month: July ('Jul')

Pandas Series allows slicing using index labels. When slicing by label, both the start and end labels are inclusive. This means we can directly specify 'Mar' as the start and 'Jul' as the end.

# Display months from March through July
months_3_to_7 = MonthDays['Mar':'Jul']
print("Months 3 through 7 from MonthDays:")
print(months_3_to_7)

The output shows the slice of the Series containing the specified months and their corresponding days. The question asks to "Display the names of the months", and by displaying the sliced Series, we implicitly show both the names (index) and their values.

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.