Python Pandas (Series) Important Questions Answer
Exam Special Python Pandas Assignment
Que 31. What is the output of the given python code?
>>> import pandas as pd
>>> import numpy as np
>>> array1 = np.array([1, 2, 3, 4])
>>> series = pd.Series(array1, index = [‘Jan’, ‘Feb’, ‘Mar’, ‘Apr’] )
>>> print(series)
Answer: The output of the above code is
Que 32. What is the error in the given python code? Give reason.
>>> import pandas as pd
>>> import numpy as np
>>> array1 = np.array([1, 2, 3, 4])
>>> series = pd.Series(array1, index = [‘Jan’, ‘Feb’, ‘Mar’] )
>>> print(series)
Answer: ValueError: Length of passed values is 4, index implies 3.
Reason: When index labels are passed with the array, then the length of the index and array must be of the same size, else it will result in a ValueError.
Que 33. What is the output of the given python code?
>>> import pandas as pd
>>> dict1 = {‘India’: ‘NewDelhi’, ‘UK’: ‘London’, ‘Japan’: ‘Tokyo’}
>>> series = pd.Series(dict1)
>>> print(series)
Answer: The output of the above code is:
Que 34. What is the attribute of a Series?
Answer: Every series object in pandas has certain properties. These properties are called Attributes of a Series and they can be accessed by that property.
Que 35. Which attribute can assign a name to the series? Give example
Answer: name
Example:
Que 36. Which attribute can assign a name to the index of a series? Give example
Answer: index.name attribute is used to assign a name to the index of the series.
Example:
Que 37. Explain the uses of the following attributes of Series.
(a) values (b) size (c) empty
Answer: (a) values: prints a list of the values in the series.
(b) size: prints the number of values in the Series object.
(c) empty: prints True if the series is empty, and False otherwise.
Que 38. Explain the uses of the following attributes of the Series.
(a) index (b) dtype (c) shape (d) nbytes (e) ndim (f) hasnans
Answer: (a) index: returns the index of the series.
(b) dtype : returns the dtype of object of the underlying data.
(c) shape: return a tuple of the shpae of the underlying data.
(d) nbytes: return the number of bytes in the underlying data.
(e) ndim: return the number of dimension of underlying data.
(f) hasnans: return True if there are any NaN value; otherwise return false.
Que 39. What is NaN? Create a series containing NaN value.
Answer:NaN stands for Not a Number. It is available in numpy module. To use NaN, you must imported NumPy.
import pandas as pd
import numpy as np
series2 = pd.Series([10,20,np.NaN,30])
print(series2)
Que 40. Write commands to print the details of Series Object serObj –
- (a) If the series is empty
- (b) Indexes of the series
- (c) The data type of underlying data
- (d) If the series stores any NaN values.
Answer:
a. serObj.empty
b. serObj.index
c. serObj.dtype
d. serObj.hasnans