Section A: Multiple Choice Questions (MCQs)
Which attribute is used to get the labels of the rows and columns in a DataFrame?
- a)
shape
- b)
index
- c)
columns
- d)
axes
- a)
The
dtype
attribute of a DataFrame is used to:- a) Find the number of dimensions
- b) Get the data type of each column
- c) Check for missing values
- d) Get the shape of the DataFrame
What does the
shape
attribute return?- a) Number of rows
- b) Number of columns
- c) A tuple representing the number of rows and columns
- d) The size of the DataFrame in memory
The
ndim
attribute of a DataFrame returns:- a) The number of rows
- b) The number of columns
- c) The number of dimensions
- d) The total number of elements
Which attribute would you use to get the total number of elements in a DataFrame?
- a)
size
- b)
shape
- c)
ndim
- d)
axes
- a)
Section B: Short Answer Questions
Index and Columns: Given the following DataFrame:
import pandas as pddata = {'A': [1, 2, 3], 'B': [4, 5, 6]}df = pd.DataFrame(data)Write the Python code to get the index labels and column names of this DataFrame.
Shape and Size: Explain the difference between the
shape
andsize
attributes in a DataFrame. Provide a code example to demonstrate their usage.Data Types: How can you find out the data type of each column in a DataFrame? Illustrate with an example.
Dimensionality: What does the
ndim
attribute tell you about a DataFrame? Provide an example using a DataFrame with more than two dimensions (e.g., a Panel).
Section C: Practical Task
Create a DataFrame with columns
'ProductID'
,'ProductName'
, and'Price'
with at least 5 rows of data. Write the Python code to:- Get the shape of the DataFrame.
- Retrieve the index and columns of the DataFrame.
- Determine the size of the DataFrame.
Given a DataFrame with mixed data types (e.g., integers, floats, and strings), write a Python function
check_dtype(df)
that prints the data type of each column.Create a DataFrame and demonstrate the use of the
axes
attribute. Explain how this attribute can be useful when manipulating data.Consider a DataFrame
df
with a large number of columns. Write a Python snippet to list all column names along with their data types,
0 Comments
Please do note create link post in comment section