Python Pandas MCQ
1). What is the purpose of the `value_counts()` function in Pandas?
a) To calculate the cumulative sum of a column
b) To count the occurrences of each unique value in a column
c) To sort a DataFrame based on a specific column
d) To remove duplicate rows from a DataFrame
Correct answer is: b) To count the occurrences of each unique value in a column
Explanation: The `value_counts()` function in Pandas is used to count the occurrences of each unique value in a column.
2). How can you calculate the difference between two dates in Pandas?
a) df.diff_dates()
b) df.difference()
c) df.calculate_difference()
d) df.subtract_dates()
Correct answer is: a) df.diff_dates()
Explanation: The `diff_dates()` function is used to calculate the difference between two dates in Pandas.
3). 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.
4). How can you calculate the median of each column in a DataFrame in Pandas?
a) df.median()
b) df.calculate_median()
c) df.column_median()
d) df.median_column()
Correct answer is: a) df.median()
Explanation: The `median()` function in Pandas is used to calculate the median of each column in a DataFrame.
5). What is the purpose of the `rename()` function in Pandas?
a) To calculate the mean of each column in a DataFrame
b) To remove duplicate rows from a DataFrame
c) To change the index labels of a DataFrame
d) To sort a DataFrame based on a specific column
Correct answer is: c) To change the index labels of a DataFrame
Explanation: The `rename()` function in Pandas is used to change the index labels of a DataFrame.
6). How can you calculate the mode of each column in a DataFrame in Pandas?
a) df.mode()
b) df.calculate_mode()
c) df.column_mode()
d) df.mode_column()
Correct answer is: a) df.mode()
Explanation: Each column in a DataFrame’s mode is determined using the Pandas’mode()’ function.
7). What is the purpose of the `cumprod()` function in Pandas?
a) To calculate the cumulative sum of a column
b) To calculate the cumulative product of a column
c) To calculate the cumulative mean of a column
d) To calculate the cumulative median of a column
Correct answer is: b) To calculate the cumulative product of a column
Explanation: The `cumprod()` function in Pandas is used to calculate the cumulative product of a column.
8). How can you calculate the maximum value of each column in a DataFrame in Pandas?
a) df.max()
b) df.maximum()
c) df.calculate_max()
d) df.column_max()
Correct answer is: a) df.max()
Explanation: The `max()` function in Pandas is used to calculate the maximum value of each column in a DataFrame.
9). What is the purpose of the `cummin()` function in Pandas?
a) To calculate the cumulative sum of a column
b) To calculate the cumulative minimum of a column
c) To calculate the cumulative mean of a column
d) To calculate the cumulative median of a column
Correct answer is: b) To calculate the cumulative minimum of a column
Explanation: The `cummin()` function in Pandas is used to calculate the cumulative minimum of a column.
10). How can you calculate the minimum value of each column in a DataFrame in Pandas?
a) df.min()
b) df.minimum()
c) df.calculate_min()
d) df.column_min()
Correct answer is: a) df.min()
Explanation: The `min()` function in Pandas is used to calculate the minimum value of each column in a DataFrame.
11). How can you select multiple columns from a DataFrame in Pandas?
a) df.select_columns()
b) df.columns()
c) df.get_columns()
d) df[[‘column1’, ‘column2’]]
Correct answer is: d) df[[‘column1’, ‘column2’]]
Explanation: To select multiple columns from a DataFrame in Pandas, you can use the double square bracket notation.
12). How can you calculate the standard deviation of each column in a DataFrame in Pandas?
a) df.std()
b) df.standard_deviation()
c) df.calculate_std()
d) df.column_std()
Correct answer is: a) df.std()
Explanation: The `std()` function in Pandas is used to calculate the standard deviation of each column in a DataFrame.
13). What is the purpose of the `agg()` function in Pandas?
a) To aggregate data based on a specific column
b) To determine a column’s average value
c) To remove rows with empty values
d) To sort a DataFrame based on a specific column
Correct answer is: a) To aggregate data based on a specific column
Explanation: The `agg()` function in Pandas is used to aggregate data based on a specific column using a specified function or a dictionary of functions.
14). How can you calculate the skewness of each column in a DataFrame in Pandas?
a) df.skew()
b) df.calculate_skewness()
c) df.column_skewness()
d) df.skewness()
Correct answer is: a) df.skew()
Explanation: The `skew()` function in Pandas is used to calculate the skewness of each column in a DataFrame.
15). How can you calculate the kurtosis of each column in a DataFrame in Pandas?
a) df.kurtosis()
b) df.calculate_kurtosis()
c) df.column_kurtosis()
d) df.kurt()
Correct answer is: a) df.kurtosis()
Explanation: The `kurtosis()` function in Pandas is used to calculate the kurtosis of each column in a DataFrame.
16). What is the purpose of the `iterrows()` function in Pandas?
a) To iterate over the rows of a DataFrame
b) To iterate over the columns of a DataFrame
c) To iterate over the unique values of a column
d) To iterate over the index labels of a DataFrame
Correct answer is: a) To iterate over the rows of a DataFrame
Explanation: The `iterrows()` function in Pandas is used to iterate over the rows of a DataFrame.
17). How can you calculate the covariance between two columns in a DataFrame in Pandas?
a) df.calculate_covariance()
b) df.column_covariance()
c) df.cov()
d) df.covariance()
Correct answer is: c) df.cov()
Explanation: The `cov()` function in Pandas is used to calculate the covariance between two columns in a DataFrame.
18). 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.
19). How can you calculate the percentile of each column in a DataFrame in Pandas?
a) df.percentile()
b) df.calculate_percentile()
c) df.column_percentile()
d) df.quantile()
Correct answer is: d) df.quantile()
Explanation: The `quantile()` function in Pandas is used to calculate the percentile of each column in a DataFrame.
20). What is the purpose of the `rolling()` function in Pandas?
a) To calculate rolling statistics on a column
b) To remove duplicate rows from a DataFrame
c) To interpolate missing values in a DataFrame
d) To calculate the cumulative sum of a column
Correct answer is: a) To calculate rolling statistics on a column
Explanation: The `rolling()` function in Pandas is used to calculate rolling statistics, such as the rolling mean or rolling sum, on a column.
21). How can you calculate the exponential moving average of a column in a DataFrame in Pandas?
a) df.calculate_ema()
b) df.ema()
c) df.exp_moving_average()
d) df.ewm()
Correct answer is: d) df.ewm()
Explanation: The `ewm()` function in Pandas is used to calculate the exponential moving average of a column in a DataFrame.
22). What is the purpose of the `stack()` function in Pandas?
a) To stack multiple DataFrames vertically
b) To stack multiple DataFrames horizontally
c) To stack multiple columns into a single column
d) To stack multiple rows into a single row
Correct answer is: c) To stack multiple columns into a single column
Explanation: The `stack()` function in Pandas is used to stack multiple columns into a single column.
23). How can you calculate the weighted average of a column in a DataFrame in Pandas?
a) df.calculate_weighted_average()
b) df.weighted_avg()
c) df.column_weighted_average()
d) df.dot_product()
Correct answer is: b) df.weighted_avg()
Explanation: There is no built-in weighted average function in Pandas. However, you can calculate the weighted average by multiplying the values of a column by their corresponding weights and then dividing the sum by the sum of the weights.
24). What is the purpose of the `squeeze()` function in Pandas?
a) To remove a single-dimensional axis from a DataFrame
b) To compress the data in a DataFrame to reduce memory usage
c) To remove duplicate rows from a DataFrame
d) To sort a DataFrame based on a specific column
Correct answer is: a) To remove a single-dimensional axis from a DataFrame
Explanation: The `squeeze()` function in Pandas is used to remove a single-dimensional axis from a DataFrame, resulting in a Series if applicable.
25). What is the purpose of the melt() function in Pandas?
a) To merge multiple DataFrames based on a common column
b) To transpose the rows and columns of a DataFrame
c) To reshape a DataFrame from wide to long format
d) To calculate the median of each column in a DataFrame
Correct answer is: c) To reshape a DataFrame from wide to long format
Explanation: The melt() function in Pandas is used to reshape a DataFrame from wide to long format by unpivoting the data based on specified columns.