Q. 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])


Answer :-

The error in the above code is in the indexing of the s series object. Instead of providing a single index or a list of indexes to extract the corresponding values, the code is using comma-separated values, which is not a valid way to index a Series  

 To fix this error, you should use a single index or a list of indexes enclosed in square brackets to extract the values.

import numpy as np
import pandas as pd

data = np.array(['a', 'b', 'c', 'd', 'e', 'f'])
s = pd.Series(data, index=[100, 101, 102, 103, 104, 105])

# get values using a single index
print(s[102])  # output: c

# get values using a list of indexes
print(s[[102, 103, 104]])  # output:
# 102    c
# 103    d
# 104    e
# dtype: object

Post a Comment

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

Previous Post Next Post