Informatics Practices
Why does following code cause error ?
s1 = pd.Series(range(1, 15, 3), index = list('abcd'))
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.
Related Questions
Find the error in following code fragment :
S2 = pd.Series([1, 2, 3, 4], index = range(4))Find the Error :
data = np.array(['a', 'b', 'c', 'd', 'e', 'f']) s = pd.Series(data, index = [100, 101, 102, 103, 104, 105]) print(s[102, 103, 104] )Can you correct the error ?
Why does following code cause error ?
s1 = pd.Series(range(1, 15, 3), index = list('ababa')) print(s1['ab'])If Ser is a Series type object having 30 values, then how are statements (a), (b) and (c), (d) similar and different ?
(a) print(Ser.head())
(b) print(Ser.head(8))
(c) print(Ser.tail())
(d) print(Ser.tail(11))