Informatics Practices
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)
Python Pandas
1 Like
Answer
(a)
Series([], dtype: int64)
Working
The slice S[1:1] starts at index 1 and ends at index 1, but because the end index is exclusive, it does not include any elements, resulting in an empty Series.
(b)
pencils 20
dtype: int64
Working
The slice S[0:1] starts at index 0 and ends at index 1, but because the end index is exclusive, it includes only one element i.e., the element at index 0.
(c)
pencils 20
notebooks 33
dtype: int64
Working
The slice S[0:2] starts at index 0 and ends at index 1, hence, it includes two elements i.e., elements from index 0 and 1.
(d)
pencils 12
notebooks 12
scales 52
erasers 10
dtype: int64
Working
The slice S[0:2] = 12 assigns the value 12 to indices 0 and 1 in Series S, directly modifying those elements. The updated Series is then printed.
(e)
Index(['pencils', 'notebooks', 'scales', 'erasers'], dtype = 'object')
[20 33 52 10]
Working
The code print(S.index) displays the indices of Series S, while print(S.values) displays the values of Series.
Answered By
2 Likes
Related Questions
Consider the same Series object, S, given in the previous question. What output will be produced by following code fragment ?
S.index = ['AMZN', 'AAPL', 'MSFT', 'GOOG'] print(S) print(S['AMZN']) S['AMZN'] = 1.5 print(S['AMZN']) print(S)What will be the output produced by the following code ?
Stationery = ['pencils', 'notebooks', 'scales', 'erasers'] S = pd.Series([20, 33, 52, 10], index = Stationery) S2 = pd.Series([17, 13, 31, 32], index = Stationery) print(S + S2) S = S + S2 print(S + S2)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 :
S2 = pd.Series([101, 102, 102, 104]) print(S2.index) S2.index = [0, 1, 2, 3, 4, 5] S2[5] = 220 print(S2)