Informatics Practices
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 ?
Answer
The error in the above code is in the line print(s[102, 103, 104]). When accessing elements in a pandas Series using square brackets, we should use a list of index values, not multiple separate index values separated by commas.
The corrected code is:
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]])
Related Questions
Find the error in following code fragment
S1 = pd.Series(1, 2, 3, 4, index = range(7))Find the error in following code fragment :
S2 = pd.Series([1, 2, 3, 4], index = range(4))Why does following code cause error ?
s1 = pd.Series(range(1, 15, 3), index = list('abcd'))Why does following code cause error ?
s1 = pd.Series(range(1, 15, 3), index = list('ababa')) print(s1['ab'])