Skip to content
Exercises · Q5

Q.Create the following Series and do the specified operations:

a) EngAlph, having 26 elements with the alphabets as values and default index values.
b) Vowels, having 5 elements with index labels 'a', 'e', 'i', 'o' and 'u' and all the five values set to zero. Check if it is an empty series.
c) Friends, from a dictionary having roll numbers of five of your friends as data and their first name as keys.
d) MTseries, an empty Series. Check if it is an empty series.
e) MonthDays, from a numpy array having the number of days in the 12 months of a year. The labels should be the month numbers from 1 to 12.
Punjab PsebTextbookSubjective· 5mImportance★★★★★
34% · 13/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 create Pandas Series in Python using various data sources like lists, dictionaries, and NumPy arrays, with both default and custom index labels. It also covers checking if a Series is empty.

In Pandas, a Series is a one-dimensional labeled array capable of holding any data type (integers, strings, floating point numbers, Python objects, etc.). It's essentially a column in a spreadsheet or a single column of a DataFrame. The key feature of a Series is its index, which provides labels for each element, allowing for easy access and alignment of data.

We will create several Series based on different requirements, showcasing the flexibility of the pandas.Series() constructor.

import pandas as pd
import numpy as np

a) EngAlph, having 26 elements with the alphabets as values and default index values.

To create a Series with alphabets as values and default integer indices, we first need a list of all English alphabets. The pd.Series() constructor can directly take a list as its data argument. When no index is explicitly provided, Pandas automatically generates a default integer index starting from 0.

# Create a list of English alphabets
alphabets = [chr(i) for i in range(ord('a'), ord('z') + 1)]

# Create the Series EngAlph
EngAlph = pd.Series(data=alphabets)

print("EngAlph Series:")
print(EngAlph)
EngAlph Series:
0     a
1     b
2     c
3     d
4     e
5     f
6     g
7     h
8     i
9     j
10    k
11    l
12    m
13    n
14    o
15    p
16    q
17    r
18    s
19    t
20    u
21    v
22    w
23    x
24    y
25    z
dtype: object

Explanation:

  • alphabets = [chr(i) for i in range(ord('a'), ord('z') + 1)] generates a list of lowercase English alphabets. ord() returns the Unicode code point for a character, and chr() converts a code point back to a character.
  • pd.Series(data=alphabets) creates the Series. Since index is not specified, it defaults to 0, 1, 2, ..., 25. The dtype: object indicates that the Series contains Python objects, in this case, strings.

b) Vowels, having 5 elements with index labels 'a', 'e', 'i', 'o' and 'u' and all the five values set to zero. Check if it is an empty series.

Here, we need to specify both the values and custom index labels. We'll create a list of five zeros for the values and a list of the vowel characters for the index. The pd.Series() constructor allows us to pass these two lists using the data and index arguments, respectively. After creating the Series, we will use the .empty attribute to check if it contains any elements.

# Values for the Series (all zeros)
vowel_values = [0, 0, 0, 0, 0]

# Custom index labels
vowel_indices = ['a', 'e', 'i', 'o', 'u']

# Create the Series Vowels
Vowels = pd.Series(data=vowel_values, index=vowel_indices)

print("\nVowels Series:")
print(Vowels)

# Check if it is an empty series
is_vowels_empty = Vowels.empty
print(f"\nIs Vowels an empty series? {is_vowels_empty}")
Vowels Series:
a    0
e    0
i    0
o    0
u    0
dtype: int64

Is Vowels an empty series? False

Explanation:

  • pd.Series(data=vowel_values, index=vowel_indices) explicitly maps the vowel_values to the vowel_indices.
  • Vowels.empty is a boolean attribute that returns True if the Series has no elements (i.e., its length is 0), and False otherwise. Since Vowels has 5 elements, it returns False.

c) Friends, from a dictionary having roll numbers of five of your friends as data and their first name as keys.

When creating a Series from a dictionary, Pandas automatically uses the dictionary's keys as the Series' index labels and the dictionary's values as the Series' data values. This is a convenient way to create a Series with meaningful labels directly.

# Create a dictionary with friend names as keys and roll numbers as values
friends_data = {
    'Alice': 101,
    'Bob': 102,
    'Charlie': 103,
    'David': 104,
    'Eve': 105
}

# Create the Series Friends
Friends = pd.Series(data=friends_data)

print("\nFriends Series:")
print(Friends)
Friends Series:
Alice      101
Bob        102
Charlie    103
David      104
Eve        105
dtype: int64

Explanation:

  • pd.Series(data=friends_data) directly converts the dictionary. The keys ('Alice', 'Bob', etc.) become the index, and the values (101, 102, etc.) become the data.

d) MTseries, an empty Series. Check if it is an empty series.

To create an empty Series, you can call pd.Series() without any data or index arguments. It's good practice to specify a dtype for an empty Series, as Pandas needs to know what kind of data it would hold if it weren't empty. We will then use the .empty attribute to confirm it is indeed empty.

# Create an empty Series MTseries
# Specifying dtype is good practice for empty Series
MTseries = pd.Series(dtype=object)

print("\nMTseries (empty Series):")
print(MTseries)

# Check if it is an empty series
is_mtseries_empty = MTseries.empty
print(f"\nIs MTseries an empty series? {is_mtseries_empty}")
MTseries (empty Series):
Series([], dtype: object)

Is MTseries an empty series? True

Explanation:

  • pd.Series(dtype=object) creates a Series with no elements. The dtype=object specifies that if elements were added, they would be Python objects (a generic type). …

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.