Informatics Practices
What will be the output produced by the following codes, considering the Series object S given in Q.13?
(a) print(S[1:4])
(b) print(S[:1])
(c) print(S[0:2])
(d) S[0:2] = 12
print(S)
(e) print(S.index)
(f) print(S.values)
The Series object 'S' is as follows:
pencils 20
notebooks 33
scales 52
erasers 10
dtype: int64
Python Data Handling
2 Likes
Answer
(a) print(S[1:4])
notebooks 33
scales 52
erasers 10
dtype: int64
Working
The slice S[1:4] starts at index 1 and ends at index 3, hence, it includes three elements i.e., elements from index 1 and 3.
(b) print(S[:1])
pencils 20
dtype: int64
Working
The slice S[: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) print(S[0:2])
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) S[0:2] = 12
print(S)
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) print(S.index)
Index(['pencils', 'notebooks', 'scales', 'erasers'], dtype='object')
Working
The code print(S.index) displays the indices of Series S.
(f) print(S.values)
[12 12 52 10]
Working
The code print(S.values) displays the values of Series S.
Answered By
2 Likes
Related Questions
Consider the following series object namely S:
0 0.430271 1 0.617328 2 0.265421 3 0.836113 dtype: float64What will be returned by the following statements?
(a) S * 100
(b) S > 0
(c) S1 = pd.Series(S)
(d) S3 = pd.Series(S1) + 3
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)Write a program to iterate and print a dataframe column-wise and print only first three columns.
Write a program to iterate and print a dataframe row-wise at a time and print only first five rows.