Skip to content
Exercises · Q6

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

a) Set all the values of Vowels to 10 and display the Series.
b) Divide all values of Vowels by 2 and display the Series.
c) Create another series Vowels1 having 5 elements with index labels 'a', 'e', 'i', 'o' and 'u' having values [2,5,6,3,8] respectively.
d) Add Vowels and Vowels1 and assign the result to Vowels3.
e) Subtract, Multiply and Divide Vowels by Vowels1.
f) Alter the labels of Vowels1 to ['A', 'E', 'I', 'O', 'U'].
CBSENCERTSubjective· 5mImportance★★★★★
37% · 14/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 →

Six one-line pandas commands on a Series. Vowels[:] = 10 broadcasts a scalar to every element; Vowels / 2 makes every value 5.0 (and converts the Series to float64); a second Series Vowels1 = [2, 5, 6, 3, 8] is created on the same labels; the four arithmetic operators then work element-wise, aligned on the index labels; and Vowels1.index = [...] renames the labels in place. The key idea throughout is index alignment — pandas matches values by label, never by position.

The starting point (from Question 5)

Question 5 created a Series named Vowels with five elements, index labels 'a', 'e', 'i', 'o', 'u', all values set to 0:

import pandas as pd

Vowels = pd.Series([0, 0, 0, 0, 0], index=['a', 'e', 'i', 'o', 'u'])
print(Vowels)

Output

a    0
e    0
i    0
o    0
u    0
dtype: int64

(a) Set all the values of Vowels to 10

Vowels[:] = 10
print(Vowels)

Output

a    10
e    10
i    10
o    10
u    10
dtype: int64
Important

Vowels[:] = 10 assigns into every existing element — this is broadcasting a scalar across the slice. Writing Vowels = 10 would be quite different: it would throw the Series away and rebind the name to the plain integer 10.

(b) Divide all values of Vowels by 2

Vowels = Vowels / 2
print(Vowels)

Output

a    5.0
e    5.0
i    5.0
o    5.0
u    5.0
dtype: float64

Note the dtype change from int64 to float64: / in Python 3 is true division and always produces a float. (Vowels // 2 would keep integers.)

(c) Create Vowels1

Vowels1 = pd.Series([2, 5, 6, 3, 8], index=['a', 'e', 'i', 'o', 'u'])
print(Vowels1)

Output

a    2
e    5
i    6
o    3
u    8
dtype: int64

(d) Add Vowels and Vowels1 into Vowels3

Vowels3 = Vowels + Vowels1
print(Vowels3)

Output

a     7.0
e    10.0
i    11.0
o     8.0
u    13.0
dtype: float64

Element by element: 5.0 + 2 = 7.0, 5.0 + 5 = 10.0, 5.0 + 6 = 11.0, 5.0 + 3 = 8.0, 5.0 + 8 = 13.0.

Note

Index alignment. Pandas adds the value at label 'a' in one Series to the value at label 'a' in the other — it matches on the label, not on the position. If Vowels1 had been indexed ['e','a','i','o','u'], the result would be exactly the same, because alignment ignores order. Any label present in only one Series would give NaN.

(e) Subtract, multiply and divide Vowels by Vowels1

print(Vowels - Vowels1)
print(Vowels * Vowels1)
print(Vowels / Vowels1)

Output

a    3.0
e    0.0
i   -1.0
o    2.0
u   -3.0
dtype: float64

a    10.0
e    25.0
i    30.0
o    15.0
u    40.0
dtype: float64

a    2.500000
e    1.000000
i    0.833333
o    1.666667
u    0.625000
dtype: float64

All three, worked out element-wise from Vowels = 5.0 everywhere:

LabelVowelsVowels1−×÷
a5.023.010.05.0 / 2 = 2.500000
e5.050.025.05.0 / 5 = 1.000000
i5.06−1.030.05.0 / 6 = 0.833333
o5.032.015.05.0 / 3 = 1.666667
u5.08−3.040.05.0 / 8 = 0.625000

The same operations can be written as methods, which additionally allow a fill_value for missing labels:

print(Vowels.sub(Vowels1))
print(Vowels.mul(Vowels1))
print(Vowels.div(Vowels1))

These produce identical output to -, * and /.

(f) Alter the labels of Vowels1

Vowels1.index = ['A', 'E', 'I', 'O', 'U']
print(Vowels1)

Output

A    2
E    5
I    6
O    3
U    8
dtype: int64
Watch out

Assigning to .index renames the labels in place — the values stay where they are. It is not a rename mapping: the new list must have exactly as many labels as there are elements (5 here), in the order you want them applied. After this, Vowels + Vowels1 would produce ten NaN rows, because no label of Vowels (a, e, i, o, u) matches any label of Vowels1 (A, E, I, O, U) — a vivid demonstration of index alignment.

The complete program …

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.