Q. Given a Series object s13 as shown below:


A    7600
B    5600
C    7000
D    7000
dtype: int64

Why is following code producing Error while working on Series object s13 ?


import pandas as pd

s13.index = range(0, 5)

print (s13)


Answer :-

The given code is producing error because the s13 object has 4 elements only and the code line:

s13.index = range(0, 5)

is trying to assign 5 new indexes to it: range(0, 5) produces 5 values as 0, 1, 2, 3, 4.
We can change the indexes of a Series object only if the new indexes are same in count as original and that is why the given code is producing an error.

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post