Python Pandas MCQ Questions and Answers

1. What is Pandas primarily used for in Python?

a) Web development
b) Machine learning
c) Data manipulation and analysis
d) Game development

Answer:

c) Data manipulation and analysis

Explanation:

Pandas is a software library written for the Python programming language for data manipulation and analysis.

2. Which data structure in Pandas is used for 1-dimensional labeled data?

a) DataFrame
b) Series
c) Matrix
d) Array

Answer:

b) Series

Explanation:

Series is a one-dimensional labeled array capable of holding any data type.

3. How do you read a CSV file using Pandas?

a) pandas.open_csv('file.csv')
b) pandas.read('file.csv', 'csv')
c) pandas.read_csv('file.csv')
d) pandas.csv('file.csv')

Answer:

c) pandas.read_csv('file.csv')

Explanation:

The read_csv function in Pandas is used to read a CSV file and convert it into a DataFrame.

4. How can you access the first five rows of a DataFrame named 'df'?

a) df.first(5)
b) df[0:5]
c) df.head(5)
d) df.rows(5)

Answer:

c) df.head(5)

Explanation:

The head() function in Pandas is used to return the first 'n' rows for the DataFrame, with 5 being the default.

5. What is the purpose of the function df.describe() in Pandas?

a) To describe the syntax of the DataFrame 'df'
b) To print a concise summary of the DataFrame 'df'
c) To return descriptive statistics of the DataFrame 'df'
d) To rename the DataFrame 'df'

Answer:

c) To return descriptive statistics of the DataFrame 'df'

Explanation:

The describe() function in Pandas provides descriptive statistics including mean, median, mode, standard deviation, etc., of the data in the DataFrame.

6. How do you check for missing values in a DataFrame 'df'?

a) df.isnull()
b) df.missing()
c) df.has_null()
d) df.null()

Answer:

a) df.isnull()

Explanation:

The isnull() function in Pandas is used to detect missing values, returning a DataFrame of boolean values (True if missing).

7. Which method is used to change the index of a DataFrame?

a) df.set_index()
b) df.index()
c) df.change_index()
d) df.modify_index()

Answer:

a) df.set_index()

Explanation:

The set_index() method is used to set the DataFrame index using existing columns.

8. How do you concatenate two DataFrames in Pandas?

a) pd.concat([df1, df2])
b) pd.append(df1, df2)
c) pd.merge(df1, df2)
d) pd.join(df1, df2)

Answer:

a) pd.concat([df1, df2])

Explanation:

The concat() function in Pandas is used to concatenate pandas objects along a particular axis.

9. What is the default join type in the merge() function in Pandas?

a) Inner
b) Outer
c) Left
d) Right

Answer:

a) Inner

Explanation:

The default join type in the merge() function is 'inner', which returns only the rows with matching keys in both DataFrames.

10. How do you drop rows with missing values in a DataFrame named 'df'?

a) df.dropna()
b) df.drop_null()
c) df.remove_na()
d) df.delete_missing()

Answer:

a) df.dropna()

Explanation:

The dropna() method in Pandas is used to remove missing values (NaN).

11. How can you group data in a DataFrame 'df' by column 'A'?

a) df.groupby('A')
b) df.group('A')
c) df.cluster('A')
d) df.collect('A')

Answer:

a) df.groupby('A')

Explanation:

The groupby() method in Pandas is used to group a DataFrame using a mapper or by a series of columns.

12. What does df.iloc[2:5, 1:3] return?

a) All rows between 2 and 5 and columns between 1 and 3
b) Rows at positions 2 through 4 and columns at positions 1 and 2
c) Rows 2 through 5 and columns 1 through 3
d) Rows at positions 2 and 5 and columns at positions 1 and 3

Answer:

b) Rows at positions 2 through 4 and columns at positions 1 and 2

Explanation:

iloc is integer-index based, so df.iloc[2:5, 1:3] selects rows 2 to 4 (5 not included) and columns 1 to 2 (3 not included).

13. How do you sort a DataFrame 'df' by column 'A'?

a) df.sort('A')
b) df.sort_values(by='A')
c) df.order('A')
d) df.arrange('A')

Answer:

b) df.sort_values(by='A')

Explanation:

The sort_values() method is used in Pandas to sort a DataFrame by the values along either axis.

14. What is the function of the pivot_table method in Pandas?

a) To reshape DataFrames into a more readable format
b) To pivot the orientation of the DataFrame
c) To create a new DataFrame based on column values
d) To create a spreadsheet-style pivot table as a DataFrame

Answer:

d) To create a spreadsheet-style pivot table as a DataFrame

Explanation:

The pivot_table method in Pandas is used to create a new derived table out of an existing one, summarizing the data in a spreadsheet-style format.

15. How do you select a column named 'A' in a DataFrame 'df'?

a) df['A']
b) df.A
c) Both a) and b)
d) df(select='A')

Answer:

c) Both a) and b)

Explanation:

In Pandas, a column in a DataFrame can be selected using the df['A'] notation or as an attribute df.A.

16. How can you create a new DataFrame from a dictionary?

a) pd.DataFrame.from_dict(my_dict)
b) pd.DataFrame(my_dict)
c) pd.create_dataframe(my_dict)
d) pd.new(my_dict)

Answer:

b) pd.DataFrame(my_dict)

Explanation:

A new DataFrame can be created from a dictionary using the pd.DataFrame() constructor.

17. How do you change the data type of a column in a DataFrame?

a) df.cast_column()
b) df.convert_dtype()
c) df.astype()
d) df.change_type()

Answer:

c) df.astype()

Explanation:

The astype() method in Pandas is used to cast a pandas object to a specified dtype.

18. Which method is used to perform an SQL-style join of two DataFrames?

a) pd.join()
b) pd.merge()
c) pd.concat()
d) pd.combine()

Answer:

b) pd.merge()

Explanation:

The merge() function in Pandas is used to merge DataFrames in an SQL-style join operation.

19. How can you detect duplicate rows in a DataFrame 'df'?

a) df.duplicated()
b) df.duplicates()
c) df.has_duplicates()
d) df.check_duplicates()

Answer:

a) df.duplicated()

Explanation:

The duplicated() method in Pandas returns boolean series denoting duplicate rows.

20. What is the result of the operation df.mean()?

a) The median of each column in the DataFrame 'df'
b) The mean of the entire DataFrame 'df'
c) The mean of each column in the DataFrame 'df'
d) The mode of each column in the DataFrame 'df'

Answer:

c) The mean of each column in the DataFrame 'df'

Explanation:

The mean() method in Pandas returns the mean of the values for the requested axis, which is column-wise by default.

21. How can you get a summary of the memory usage of a DataFrame 'df'?

a) df.memory_usage()
b) df.memory()
c) df.mem_usage()
d) df.use_memory()

Answer:

a) df.memory_usage()

Explanation:

The memory_usage() method in Pandas returns a Series with the memory usage of each column in bytes.

22. How do you change the name of a column in a DataFrame 'df' from 'old_name' to 'new_name'?

a) df.rename(columns={'old_name': 'new_name'})
b) df.columns['old_name'] = 'new_name'
c) df.rename('old_name', 'new_name')
d) df.set_column_name('old_name', 'new_name')

Answer:

a) df.rename(columns={'old_name': 'new_name'})

Explanation:

The rename() method in Pandas is used to alter axes labels (like column names), where you pass a dictionary mapping old names to new names.

23. How can you convert a Series or a list to a DataFrame in Pandas?

a) pd.DataFrame.from_series(my_series)
b) pd.as_dataframe(my_series)
c) pd.DataFrame(my_series)
d) pd.to_dataframe(my_series)

Answer:

c) pd.DataFrame(my_series)

Explanation:

To convert a Series or a list to a DataFrame, you can simply pass it to the pd.DataFrame() constructor.

24. How do you calculate the cumulative sum of a column 'A' in a DataFrame 'df'?

a) df['A'].cumsum()
b) df['A'].sum(cumulative=True)
c) df.cumulative_sum('A')
d) df['A'].total_sum()

Answer:

a) df['A'].cumsum()

Explanation:

The cumsum() method in Pandas is used to compute the cumulative sum of a DataFrame or Series, particularly useful for creating a cumulative sum column. 25. What is the purpose of the apply() function in Pandas? a) To iterate over rows of a DataFrame b) To merge two DataFrames c) To apply a function along an axis of the DataFrame d) To apply conditional formatting to a DataFrame

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top