chapter 2 Q & A
chapter 2 Q & A
A Series is a one-dimensional labeled array in pandas capable of holding data of any type.
Difference:
• 1-D Array (NumPy): Homogeneous data, no labels.
• List (Python): Can hold mixed data, no labels.
• Dictionary: Key-value pairs, but unordered (until Python 3.7+).
• Series combines the best of both: it has labels (like a dictionary) and behaves like an array for
computations.
5. Create Series:
import pandas as pd
import numpy as np
import string
# a)EngAlph = pd.Series(list(string.ascii_lowercase))
# b)Vowels = pd.Series(0, index=['a', 'e', 'i', 'o', 'u'])
print(Vowels.empty) # False
# c)Friends = pd.Series({
'Amit': 101,
'Sana': 102,
'Ravi': 103,
'Tina': 104,
'Neha': 105})
# d)MTseries = pd.Series(dtype='float64')
print(MTseries.empty) # True
# e)MonthDays = pd.Series(np.array([31,28,31,30,31,30,31,31,30,31,30,31]), index=range(1,13))
6. Operations on Series:
# a)Vowels[:] = 10
print(Vowels)
# b)Vowels = Vowels / 2
print(Vowels)
# c)Vowels1 = pd.Series([2,5,6,3,8], index=['a','e','i','o','u'])
# d)Vowels3 = Vowels + Vowels1
# e)print(Vowels - Vowels1)
print(Vowels * Vowels1)
print(Vowels / Vowels1)
# f)Vowels1.index = ['A','E','I','O','U']