Informatics Practices

Why does following code cause error ?

s1 = pd.Series(range(1, 15, 3), index = list('abcd'))

Python Pandas

4 Likes

Answer

The code causes an error because the length of the data (range(1, 15, 3)) and the length of the index (list('abcd')) do not match. The range(1, 15, 3) generates the sequence [1, 4, 7, 10, 13], which has a length of 5. The list('abcd') generates the list ['a', 'b', 'c', 'd'], which has a length of 4. When creating a pandas Series, the length of the data and the length of the index must be the same.

Answered By

1 Like


Related Questions