Informatics Practices
Find the error in the following code fragments:
S2 = pd.Series([101, 102, 1-2, 104])
print (S2.index)
S2.index = [0.1.2.3, 4, 5]
S2[5] = 220
print (S2)
Python Data Handling
1 Like
Answer
S2 = pd.Series([101, 102, 1-2, 104])
print(S2.index)
S2.index = [0.1.2.3, 4, 5] #Error 1
S2[5] = 220
print(S2)
Error 1 — In the line S2.index = [0.1.2.3, 4, 5], the index values are not separated by commas. It should be S2.index = [0, 1, 2, 3, 4, 5]. Additionally, 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, 1-2, 104])
print(S2.index)
S2.index = [0, 1, 2, 3]
S2[5] = 220
print(S2)
Answered By
1 Like
Related Questions
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.
Find the error in the following code fragments:
S = pd.Series(2, 3, 4, 55, index = range (4))Find the error in following code fragment
S1 = pd.Series(1, 2, 3, 4, index = range(7))