Python Pandas MCQ
1). What is the purpose of the `iloc` attribute in Pandas?
a) To access rows and columns of a DataFrame by their index location
b) To access rows and columns of a DataFrame by their label
c) To access rows and columns of a DataFrame by their data type
d) To access rows and columns of a DataFrame randomly
Correct answer is: a) To access rows and columns of a DataFrame by their index location
Explanation: The `iloc` attribute in Pandas is used to access rows and columns of a DataFrame by their index location (integer position).
2). How can you calculate the correlation between columns in a DataFrame in Pandas?
a) df.corr()
b) df.correlation()
c) df.calculate_correlation()
d) df.stats.correlation()
Correct answer is: a) df.corr()
Explanation: The correlation between columns in a DataFrame is determined using the ‘corr()’ function in the Pandas programming language.
3). What is the purpose of the `dtypes` attribute in Pandas?
a) To calculate descriptive statistics of a DataFrame
b) To access the data types of each column in a DataFrame
c) To convert the data types of a DataFrame
d) To filter rows based on a condition
Correct answer is: b) To access the data types of each column in a DataFrame
Explanation: The `dtypes` attribute in Pandas is used to access the data types of each column in a DataFrame.
4). How can you apply a filter to a DataFrame based on multiple conditions in Pandas?
a) df.filter(condition1, condition2)
b) df.where(condition1, condition2)
c) df.loc[condition1, condition2]
d) df.apply(condition1, condition2)
Correct answer is: c) df.loc[condition1, condition2]
Explanation: You can apply a filter to a DataFrame based on multiple conditions using the `df.loc[condition1, condition2]` syntax in Pandas.
5). What is the purpose of the `to_datetime()` function in Pandas?
a) To convert a DataFrame to a datetime format
b) To convert a string to a datetime object
c) To calculate the difference between two datetime objects
d) To extract specific components from a datetime object
Correct answer is: b) To convert a string to a datetime object
Explanation: The `to_datetime()` function in Pandas is used to convert a string to a datetime object, enabling date and time operations.
6). How can you resample time series data in Pandas?
a) df.resample()
b) df.time_resample()
c) df.sample_time()
d) df.time_sample()
Correct answer is: a) df.resample()
Explanation: The `resample()` function in Pandas is used to resample time series data, such as converting daily data to monthly data)
7). What is the purpose of the `shift()` function in Pandas?
a) To shift the index labels of a DataFrame
b) To shift the values in a DataFrame by a specified number of periods
c) To shift the columns of a DataFrame
d) To shift the rows of a DataFrame
Correct answer is: b) To shift the values in a DataFrame by a specified number of periods
Explanation: To move the values in a DataFrame by a defined number of periods, use Pandas’ “shift()” function.
8). How can you apply a function to groups in a DataFrame in Pandas?
a) df.groupby().apply()
b) df.group().apply()
c) df.group_by().apply()
d) df.groupby().function()
Correct answer is: a) df.groupby().apply()
Explanation: You can apply a function to groups in a DataFrame using the `groupby().apply()` syntax in Pandas.
9). What is the purpose of the `duplicated()` function in Pandas?
a) To remove duplicate rows from a DataFrame
b) To identify duplicate values in a DataFrame
c) To drop rows with missing values
d) To sort a DataFrame based on a specific column
Correct answer is: b) To identify duplicate values in a DataFrame
Explanation: The `duplicated()` function in Pandas is used to identify duplicate values in a DataFrame.
10). How can you change the data type of a column in a DataFrame in Pandas?
a) df.change_dtype(column_name, new_type)
b) df.column_name = new_type
c) df.astype(column_name, new_type)
d) df.change_type(column_name, new_type)
Correct answer is: c) df.astype(column_name, new_type)
Explanation: The `astype()` function is used to change the data type of a column in a DataFrame in Pandas.
11). What is the purpose of the `cut()` function in Pandas?
a) To divide a continuous variable into discrete bins
b) To calculate the cumulative sum of a column
c) To transform a categorical variable into numerical codes
d) To create a histogram of a column
Correct answer is: a) To divide a continuous variable into discrete bins
Explanation: The `cut()` function in Pandas is used to divide a continuous variable into discrete bins or intervals.
12). How can you create a new column based on existing columns in a DataFrame in Pandas?
a) df.add_column()
b) df.create_column()
c) df.new_column()
d) df[column_name] = expression
Correct answer is: d) df[column_name] = expression
Explanation: You can create a new column based on existing columns in a DataFrame by assigning a new column name and an expression to it.
13). What is the purpose of the `transform()` function in Pandas?
a) To apply a function to each element in a DataFrame
b) To calculate summary statistics for each group in a DataFrame
c) To transform a column by replacing values based on a condition
d) To reshape the structure of a DataFrame
Correct answer is: b) To calculate summary statistics for each group in a DataFrame
Explanation: The `transform()` function in Pandas is used to calculate summary statistics for each group in a DataFrame.
14). How can you drop duplicate rows from a DataFrame in Pandas?
a) df.drop_duplicates()
b) df.remove_duplicates()
c) df.delete_duplicates()
d) df.drop_rows_duplicates()
Correct answer is: a) df.drop_duplicates()
Explanation: The `drop_duplicates()` method is used to drop duplicate rows from a DataFrame in Pandas.
15). What is the purpose of the `isnull()` function in Pandas?
a) To check if values in a DataFrame are null or missing
b) To remove null or missing values from a DataFrame
c) To replace null or missing values with a specified value
d) To interpolate null or missing values in a DataFrame
Correct answer is: a) To check if values in a DataFrame are null or missing
Explanation: The `isnull()` function in Pandas is used to check if values in a DataFrame are null or missing.
16). How can you calculate the cumulative sum of a column in a DataFrame in Pandas?
a) df.cumulative_sum()
b) df.cumsum()
c) df.sum_cumulative()
d) df.calculate_cumulative()
Correct answer is: b) df.cumsum()
Explanation: The `cumsum()` function is used to calculate the cumulative sum of a column in a DataFrame in Pandas.
17). What is the purpose of the `nunique()` function in Pandas?
a) To count the number of unique values in each column
b) To remove duplicate rows from a DataFrame
c) To calculate the mean of each column
d) To sort a DataFrame based on a specific column
Correct answer is: a) To count the number of unique values in each column
Explanation: The `nunique()` function in Pandas is used to count the number of unique values in each column of a DataFrame.
18). How can you extract specific components from a datetime column in a DataFrame in Pandas?
a) df.extract()
b) df.components()
c) df.dt.component()
d) df.get_component()
Correct answer is: c) df.dt.component()
Explanation: You can extract specific components from a datetime column in a DataFrame using the `dt.component()` syntax in Pandas.
19). What is the purpose of the `sample()` function in Pandas?
a) To randomly shuffle the rows of a DataFrame
b) To select a random sample of rows from a DataFrame
c) To calculate the mean of each column
d) To sort a DataFrame based on a specific column
Correct answer is: b) To select a random sample of rows from a DataFrame
Explanation: The `sample()` function in Pandas is used to select a random sample of rows from a DataFrame.
20). How can you calculate the mean of each column in a DataFrame in Pandas?
a) df.mean()
b) df.calculate_mean()
c) df.column_mean()
d) df.mean_column()
Correct answer is: a) df.mean()
Explanation: The `mean()` function in Pandas is used to calculate the mean of each column in a DataFrame.
21). What is the purpose of the unique() function in Pandas?
a) To calculate the unique values in a column
b) To remove duplicate rows from a DataFrame
c) To count the occurrences of each unique value in a column
d) To sort a DataFrame based on a specific column
Correct answer is: a) To calculate the unique values in a column
Explanation: The unique() function in Pandas is used to calculate the unique values in a column.
22). How can you calculate the sum of each column in a DataFrame in Pandas?
a) df.calculate_sum()
b) df.sum()
c) df.column_sum()
d) df.total()
Correct answer is: b) df.sum()
Explanation: The sum() function in Pandas is used to calculate the sum of each column in a DataFrame.
23). What is the purpose of the dropna() function in Pandas?
a) To calculate descriptive statistics of a DataFrame
b) To remove missing values from a DataFrame
c) To replace missing values with a specified value
d) To interpolate missing values in a DataFrame
Correct answer is: b) To remove missing values from a DataFrame
Explanation: The dropna() function in Pandas is used to remove missing values from a DataFrame.
24). How can you calculate the correlation matrix of a DataFrame in Pandas?
a) df.calculate_correlation()
b) df.correlation_matrix()
c) df.corr()
d) df.matrix_correlation()
Correct answer is: c) df.corr()
Explanation: The corr() function in Pandas is used to calculate the correlation matrix of a DataFrame.
25). How can you calculate the cumulative maximum of each column in a DataFrame in Pandas?
a) df.cumulative_max()
b) df.max_cumulative()
c) df.cummax()
d) df.calculate_max()
Correct answer is: c) df.cummax()
Explanation: The cummax() function in Pandas is used to calculate the cumulative maximum of each column in a DataFrame.