Python Pandas MCQs

Pandas are a Python package that is used to manipulate large data sets. Analysis, cleaning, investigating, and modifying data are some of the features available in this program. Python module pandas provide quick, versatile, and expressive data structures that are designed to enable working with "relational" or "labeled" data both easy and intuitive to use. It aspires to serve as the essential high-level building block for performing realistic, real-world data analysis in Python at the highest level of abstraction. Additionally, it aspires to be the most powerful and flexible open-source data analysis/manipulation tool available in any language, with the ability to run in any environment. Pandas make it possible to evaluate large amounts of data and provide conclusions based on statistical theory. Pandas are capable of cleaning up jumbled data sets and making them readable and relevant. In data science, it is critical to have data that is relevant.

Python Pandas MCQs: This section contains multiple-choice questions and answers on Python Pandas. These MCQs are written for beginners as well as advanced, practice these MCQs to enhance and test the knowledge of Python Pandas.

List of Python Pandas MCQs

1. What will be the output of following code?

import pandas as pd
series1 = pd.Series([10,20,30,40,50])
print (series1)
  1. 0    10
    1    20
    2    30
    3    40
    4    50
    dtype: int64
  2. 1    10
    2    20
    3    30
    4    40
    5    50
    dtype: int64
  3. 0    10
    1    20
    2    30
    3    40
    4    50
    dtype: float32
  4. None of the above mentioned

Answer: A)

0    10
1    20
2    30
3    40
4    50
dtype: int64

Explanation:

Pandas Series is a one-dimensional labeled array that may carry data of any type. It is used to store text, numbers, and other data like integers, strings, float, python objects, etc. The index labels are used to refer to the labels on the axes as a whole. Pandas Series can be considered as a column in an Excel file.

Discuss this Question


2. Observe the following code and identify what will be the output?

import pandas as pd
Series1 = pd.Series([10,20,30,40,50])
Series2 = Series1*2 
print(Series1) 
print(Series2)
  1. 1    10
    2    20
    3    30
    4    40
    5    50
    dtype: int64
    1     20
    2     40
    3     60
    3     80
    5    100
    dtype: int64
  2. 0    10
    1    20
    2    30
    3    40
    4    50
    dtype: int64
    0     20
    1     40
    2     60
    3     80
    4    100
    dtype: int64
  3. 0.   10
    1.    20
    2.    30
    3.   40
    4.    50
    dtype: int64
    0.     20
    1.     40
    2.     60
    3.     80
    4.    100
    dtype: int64
  4. 10
    20
    30
    40
    50
    dtype: int64
    20
    40
    60
    80
    100
    dtype: int64

Answer: B)

0    10
1    20
2    30
3    40
4    50
dtype: int64
0     20
1     40
2     60
3     80
4    100
dtype: int64

Explanation:

in the above code, Series1 is having a list of numbers from 10 to 50. Series2 = Series1*2, which means every element of Series1 will be multiplied by 2 and then will print.

Discuss this Question


3. What will be the output of following code?

import pandas as pnd
pnd.Series([1,2], index= ['a','b','c'])
  1. Syntax Error
  2. Index Error
  3. Value Error
  4. None of the above mentioned

Answer: C) Value Error

Explanation:

In the above code, value error will be.

Discuss this Question


4. A series object is size mutable.

  1. True
  2. False

Answer: A) True

Explanation:

A series object is size mutable. Series Objects are variable in terms of their values, but they are immutable in terms of their sizes. Vector operation refers to the fact that when we apply a function or expression to an object, it is applied to each individual item in the object.

Discuss this Question


5. A Dataframe object is value mutable.

  1. True
  2. False

Answer: A) True

Explanation:

Sequence Objects are mutable in terms of their values, but they are not mutable in terms of their sizes. When we apply a function or expression to an object, it is applied to each individual item in the object, which is known as vector operation.

Discuss this Question


6. What will be the minimum number of arguments require to pass in pandas series?

  1. 2
  2. 3
  3. 4
  4. None of the above mentioned

Answer: D) None of the above mentioned

Explanation:

There will be 1 number of arguments requires to pass in pandas series.

Discuss this Question


7. Amongst which of the following is / are used to analyze the data in pandas.

  1. Dataframe
  2. Series
  3. Both A and B
  4. None of the mentioned above

Answer: C) Both A and B

Explanation:

We can use series and dataframe to analyze the data in Pandas. Series is one one-dimensional labeled array that can store any data type like integers, strings, floating-point numbers, Python objects, etc. A DataFrame is a 2-dimensional labeled data structure with columns that can be of a variety of different kinds. We can think of it as a spreadsheet, a SQL table, or a dict of Series objects. It is one of the most widely used Pandas objects.

Discuss this Question


8. During the execution of following code, what will be the response, we get -

import pandas as pd
s =pd.Series([1,2,3,4,5],index= ['a','b','c','d','e'])
print(s['f'])
  1. KeyError
  2. IndexError
  3. ValueError
  4. None of the above mentioned

Answer: A) KeyError

Explanation:

We will get "KeyError" during the execution of above mentioned code.

Discuss this Question


9. Amongst which of the following is a correct syntax for panda's dataframe?

  1. Pandas.DataFrame(data, index, dtype, copy)
  2. pandas.DataFrame( data, index, columns, dtype, copy)
  3. pandas.DataFrame(data, index, dtype, copy)
  4. pandas.DataFrame( data, index, rows, dtype, copy)

Answer: A) pandas.DataFrame( data, index, columns, dtype, copy)

Explanation:

A syntax of pandas.DataFrame( data, index, columns, dtype, copy).

  • data - data can be represented in a variety of ways, including ndarray, series, map, lists, dict, constants, and another DataFrame.
  • index - for the row labels, the index to be used for the resulting frame is optional default index. If no index is provided, np.arange(n) is used.
  • columns - the optional default syntax for column labels is np.arange, which stands for numeric range (n). When there is no index passed, the following is true.
  • dtype - dtype identifies the data type of each column.
  • copy - this command is used for data copying if the default value is False, else it is not used.

Discuss this Question


10. Amongst which of the following can be used to create various inputs using pandas DataFrame.

  1. Lists, dict
  2. Series
  3. Numpy ndarrays and Another DataFrame
  4. All of the above mentioned

Answer: D) All of the above mentioned

Explanation:

A pandas DataFrame can be created using various inputs like Lists, dict, Series, Numpy ndarrays, Another DataFrame.

Discuss this Question


11. Observe the following code and identify what will be the output when we run following code -

Import pandas as pd
Import numpy as np  

df = pd.DataFrame(np.array([[4,6,9],[5,1,3]]))

print(df.shape)
  1. SyntaxError: invalid syntax
  2. KeyError
  3. IndexError
  4. None of the mentioned above

Answer: A) SyntaxError: invalid syntax

Explanation:

When we run the code, invalid syntax error will be reflected.

Discuss this Question


12. Amongst which of the following is / are false statement / statements -

  1. iteritems() returns each column's value in form of series object.
  2. tail() returns any number of bottom rows by specifying values of number's argument.
  3. A and B both
  4. None of the mentioned above

Answer: D) None of the mentioned above

Explanation:

The iteritems() returns each column's value in form of series object and tail() returns any number of bottom rows by specifying values of number's argument. Hence, nothing is a false statement mentioned above.

Discuss this Question


13. Observe the following code and identify what will be the output when we run following code -

import pandas as pd
df = pd.DataFrame()
print (df)
  1. Empty DataFrame
    Columns: []
    Index: []
  2. Empty Series
    Columns: [5]
    Index: [0]
  3. Empty DataFrame
    Columns: [2]
    Index: [3]
  4. None of the mentioned above

Answer: A)

Empty DataFrame
Columns: []
Index: []

Explanation:

In the above code, we dint pass any argument in pd.DataFrame() so it will give Empty DataFrame and null values in Columns: [] and Index: [].

Discuss this Question


14. Amongst which of the following is / are not correct to access individual item from dataframe 'df'.

  1. df.iat[2,2]
  2. df.loc[2,2]
  3. df.at[2,2]
  4. df[0,0]

Answer: D) df[0,0]

Explanation:

df[0,0] is incorrect.

Discuss this Question


15. Amongst which of the following is / are not an iterative function for dataframe?

  1. iterrows()
  2. itercolumns()
  3. iteritems()
  4. All of the mentioned above

Answer: B) itercolumns()

Explanation:

The itercolumns() is not an iterative function for dataframe.

Discuss this Question


16. What will be output of following code –?

import pandas as pd
data = [['Anuj',21],['Rama',25],['Kapil',22]]
df = pd.DataFrame(data,columns=['Name','Age'])
print (df)
  1.     Name  Age
    0   Anuj    21
    1   Rama   25
    2   Kapil   22
  2.      Name   Age
    0   Anuj    21
    1   Kapil   22
    2  Rama   25
    
  3.     Name   Age
    0   Kapil   22
    1   Rama   25
    2  Anuj     21
  4.     Name  Age
    0   Rama   25
    1   Anuj    21
    2   Kapil   22

Answer: A)

    Name  Age
0   Anuj    21
1   Rama   25
2   Kapil   22

Explanation:

In the above code, the data is including name and age of the candidates like [['Anuj',21],['Rama',25],['Kapil',22]]. And in the next line of the code df = pd.DataFrame(data,columns=['Name','Age']), name and age is strong in df and when we print(df), this will print the name and age of the candidates on screen.

Discuss this Question


17. What will be output of following code –?

import pandas as pd
S1=pd.Series([100,200,300,400,500],index=['A','B','C','D','E'])
S2=pd.Series([1,2,3,4,5],index=['A','B','C','D','E'])
print(S1*S2)
  1. A     10
    B     40
    C     90
    D    160
    E    250
    dtype: int64
  2. A     100
    B     400
    C     900
    D    1600
    E    2500
    dtype: int64
  3. A     1000
    B     4000
    C     9000
    D    16000
    E    25000
    dtype: int64
    
  4. A     100
    B     4000
    C     900
    D    16000
    E    2500
    dtype: int64

Answer: B)

A     100
B     400
C     900
D    1600
E    2500
dtype: int64

Explanation:

In the above code, we have taken a series of numbers of 100,200,300,400,500 and index value like 'A','B','C','D','E'. In the next line of code, Series of numbers is 1,2,3,4,5 and index value is 'A','B','C','D','E'. In the third line of code, we are multiplying series 1and series 2 so the outcome of the program will be multiplication of series numbers.

Discuss this Question


18. What will be output of following code?

import numpy as np
array1=np.array([100,200,300,400,500,600,700])
print(array1[1:5:2])
  1. [200 300]
  2. [200 700]
  3. [200 400]
  4. [200 400]

Answer: C) [200 400]

Explanation:

When we will run the code, get [200 400] as output.

Discuss this Question


19. Indexing in Series is similar to that for NumPy arrays.

  1. True
  2. False

Answer: A) True

Explanation:

Indexing in Series is analogous to indexing in NumPy arrays, and it is used to retrieve entries inside a series of elements. There are two sorts of indexes: positional indexes and labelled indexes. Positional indexes accept an integer value that corresponds to their position in the series starting from zero, whereas labelled indexes take any user-defined label as the index for the positional index.

Discuss this Question


20. DataFrame accepts many different kinds of input.

  1. True
  2. False

Answer: A) True

Explanation:

A DataFrame is a 2-dimensional labelled data structure with columns that can be of a variety of different kinds. You can think of it as a spreadsheet or a SQL table, or as a dict of Series objects arranged in a hierarchy. It is, by far, the most often encountered Pandas object. DataFrame, like Series, allows a wide variety of different types of input:

  • 1D ndarrays, lists, and dicts, as well as a series of 2-D numpy.ndarrays
  • ndarray is a structured or record ndarray
  • A Sequence of Events
  • Another DataFrame has been added.

Discuss this Question




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.