Python Pandas MCQ : Set 1

Python Pandas MCQ

1). What is Pandas?

a) A Python package for data analysis and manipulation
b) A Python framework for web development
c) A Python module for machine learning
d) A Python library for graphical plotting

Correct answer is: a) A Python package for data analysis and manipulation
Explanation: Pandas is a popular Python package used for data analysis and manipulation. It offers data structures and procedures to handle structured data effectively.

2). Which of the following data structures is commonly used in Pandas?

a) Arrays
b) Lists
c) DataFrames
d) Tuples

Correct answer is: c) DataFrames
Explanation: The primary data structure in Pandas is the DataFrame, which is a two-dimensional labeled data structure capable of holding data of different types.

3). How can you install Pandas in Python?

a) Using pip: pip install pandas
b) Using conda: conda install pandas
c) Both a and b
d) Pandas is pre-installed with Python

Correct answer is: c) Both a and b
Explanation: Pandas can be installed using either pip or conda package managers.

4). Which of the following statements is true about Series in Pandas?

a) It is a one-dimensional labeled array
b) It is a two-dimensional labeled array
c) It is a three-dimensional labeled array
d) It is a multi-dimensional labeled array

Correct answer is: a) It is a one-dimensional labeled array
Explanation: A Series is a labelled one-dimensional array that can hold any kind of data.

5). How can you access the first n rows of a DataFrame in Pandas?

a) df.head(n)
b) df.tail(n)
c) df.first(n)
d) df.last(n)

Correct answer is: a) df.head(n)
Explanation: The `head(n)` method in Pandas allows you to access the first n rows of a DataFrame.

6). What is the default index type for a DataFrame in Pandas?

a) Integer index
b) String index
c) Date index
d) Floating-point index

Correct answer is: a) Integer index
Explanation: By default, Pandas assigns an integer index to each row in a DataFrame.

7). How can you drop a column from a DataFrame in Pandas?

a) df.drop(column_name)
b) df.drop(columns=column_name)
c) df.remove(column_name)
d) df.delete(column_name)

Correct answer is: b) df.drop(columns=column_name)
Explanation: The `drop(columns=column_name)` method is used to drop a column from a DataFrame in Pandas.

8). What is the function to calculate the summary statistics of a DataFrame in Pandas?

a) df.summary()
b) df.describe()
c) df.stats()
d) df.statistics()

Correct answer is: b) df.describe()
Explanation: The `describe()` function in Pandas provides summary statistics of a DataFrame, including count, mean, standard deviation, minimum, maximum, and quartiles.

9). How can you filter rows in a DataFrame based on a condition in Pandas?

a) df.filter(condition)
b) df.where(condition)
c) df.select(condition)
d) df.loc[condition]

Correct answer is: d) df.loc[condition]
Explanation: You can filter rows in a DataFrame based on a condition using the `df.loc[condition]` syntax in Pandas.

10). What is the purpose of the `fillna()` function in Pandas?

a) To remove missing values from a DataFrame
b) To replace missing values with a specified value
c) To interpolate missing values in a DataFrame
d) To drop rows with missing values

Correct answer is: b) To replace missing values with a specified value
Explanation: The `fillna()` function is used to replace missing values in a DataFrame with a specified value in Pandas.

11). How can you sort a DataFrame by a specific column in Pandas?

a) df.sort(column_name)
b) df.sort_by(column_name)
c) df.sort_values(by=column_name)
d) df.order_by(column_name)

Correct answer is: c) df.sort_values(by=column_name)
Explanation: The `sort_values(by=column_name)` method is used to sort a DataFrame by a specific column in Pandas.

12). What is the purpose of the `groupby()` function in Pandas?

a) To filter rows based on a condition
b) To combine rows based on a specific column
c) To calculate summary statistics for each group
d) To sort the DataFrame in ascending order

Correct answer is: b) To combine rows based on a specific column
Explanation: The `groupby()` function in Pandas is used to combine rows based on a specific column, creating groups of related data)

13). How can you apply a function to each element in a DataFrame in Pandas?

a) df.apply()
b) df.map()
c) df.transform()
d) df.iterate()

Correct answer is: a) df.apply()
Explanation: The `apply()` function in Pandas is used to apply a function to each element in a DataFrame.

14). What is the purpose of the `merge()` function in Pandas?

a) To remove duplicate rows from a DataFrame
b) To combine two DataFrames based on a common column
c) To calculate the correlation between columns in a DataFrame
d) To reshape the structure of a DataFrame

Correct answer is: b) To combine two DataFrames based on a common column
Explanation: The `merge()` function in Pandas is used to combine two DataFrames based on a common column, similar to the SQL join operation.

15). What is the purpose of the `pivot_table()` function in Pandas?

a) To transpose the rows and columns of a DataFrame
b) To calculate the average value of a column in a DataFrame
c) To create a summary table based on a DataFrame’s columns
d) To reshape the structure of a DataFrame

Correct answer is: c) To create a summary table based on a DataFrame’s columns
Explanation: The `pivot_table()` function in Pandas is used to create a summary table based on a DataFrame’s columns, similar to Excel’s PivotTable functionality.

16). How can you save a DataFrame to a CSV file in Pandas?

a) df.to_csv(filename)
b) df.save_csv(filename)
c) df.write_csv(filename)
d) df.export_csv(filename)

Correct answer is: a) df.to_csv(filename)
Explanation: The `to_csv(filename)` method is used to save a DataFrame to a CSV file in Pandas.

17). What is the purpose of the `read_csv()` function in Pandas?

a) To read data from a CSV file into a DataFrame
b) To export a DataFrame to a CSV file
c) To calculate summary statistics for a DataFrame
d) To sort a DataFrame based on a specific column

Correct answer is: a) To read data from a CSV file into a DataFrame
Explanation: The `read_csv()` function in Pandas is used to read data from a CSV file and create a DataFrame.

18). How can you handle missing values in a DataFrame in Pandas?

a) df.dropna()
b) df.fillna(value)
c) df.interpolate()
d) All of the above

Correct answer is: d) All of the above
Explanation: You can handle missing values in a DataFrame in Pandas by dropping them using `dropna()`, filling them with a specified value using `fillna(value)`, or interpolating them using `interpolate()`.

19). What is the purpose of the `datetime` module in Pandas?

a) To perform date and time calculations
b) To format dates and times in a DataFrame
c) To parse date and time strings into a DataFrame
d) To figure out how much time has passed between two dates

Correct answer is: a) To perform date and time calculations
Explanation: The `datetime` module in Pandas provides functions and classes to work with dates and times, perform calculations, and handle time-related operations.

20). How can you rename the columns of a DataFrame in Pandas?

a) df.rename(columns=new_columns)
b) df.columns = new_columns
c) df.relabel(columns=new_columns)
d) df.change_columns(new_columns)

Correct answer is: a) df.rename(columns=new_columns)
Explanation: The `rename(columns=new_columns)` method is used to rename the columns of a DataFrame in Pandas.

21). What is the purpose of the pivot() function in Pandas?

a) To transpose the rows and columns of a DataFrame
b) To calculate the average value of a column in a DataFrame
c) To create a summary table based on a DataFrame’s columns
d) To reshape the structure of a DataFrame

Correct answer is: d) To reshape the structure of a DataFrame
Explanation: The pivot() function in Pandas is used to reshape the structure of a DataFrame based on the values of a column.

22). How can you convert a DataFrame into a NumPy array in Pandas?

a) df.to_array()
b) df.to_numpy()
c) df.convert_array()
d) df.as_numpy()

Correct answer is: b) df.to_numpy()
Explanation: The to_numpy() method is used to convert a DataFrame into a NumPy array in Pandas.

23). What is the purpose of the corr() function in Pandas?

a) To calculate the correlation between columns in a DataFrame
b) To calculate the covariance between columns in a DataFrame
c) To calculate the cumulative sum of a column
d) To calculate the mean of each column

Correct answer is: a) To calculate the correlation between columns in a DataFrame
Explanation: The corr() function in Pandas is used to calculate the correlation between columns in a DataFrame.

24). How can you calculate the cross-tabulation between two columns in a DataFrame in Pandas?

a) df.cross_tab()
b) df.crosstab()
c) df.tabulate()
d) df.pivot_table()

Correct answer is: b) df.crosstab()
Explanation: The crosstab() function is used to calculate the cross-tabulation between two columns in a DataFrame in Pandas.

25). What is the purpose of the rank() function in Pandas?

a) To calculate the rank of values within a column
b) To remove duplicate rows from a DataFrame
c) To determine each column’s mean
d) To sort a DataFrame based on a specific column

Correct answer is: a) To calculate the rank of values within a column
Explanation: The rank() function in Pandas is used to calculate the rank of values within a column.

Leave a Comment