Informatics Practices
Find the error in following code fragment :
S2 = pd.Series([101, 102, 102, 104])
print(S2.index)
S2.index = [0, 1, 2, 3, 4, 5]
S2[5] = 220
print(S2)
Answer
S2 = pd.Series([101, 102, 102, 104])
print(S2.index)
S2.index = [0, 1, 2, 3, 4, 5] #Error 1
S2[5] = 220
print(S2)
Error 1 — The Series S2 initially has four elements, so assigning a new index list of six elements ([0, 1, 2, 3, 4, 5]) to S2.index will raise a ValueError because the new index list length does not match the length of the Series.
The corrected code is:
S2 = pd.Series([101, 102, 102, 104])
print(S2.index)
S2.index = [0, 1, 2, 3]
S2[5] = 220
print(S2)
Related Questions
What will be the output produced by following code, considering the Series object S given above ?
(a) print(S[1:1])
(b) print(S[0:1])
(c) print(S[0:2])
(d)
S[0:2] = 12 print(S)(e)
print(S.index) print(S.values)Write a Python program to create a series object, country using a list that stores the capital of each country.
Note. Assume four countries to be used as index of the series object are India, UK, Denmark and Thailand having their capitals as New Delhi, London, Copenhagen and Bangkok respectively.
Find the error in following code fragment :
S = pd.Series(2, 3, 4, 5, index = range(4))Find the error in following code fragment
S1 = pd.Series(1, 2, 3, 4, index = range(7))