This practice paper contains various questions focusing on how to access a DataFrame with different options. It is an important topic for Class XII CBSE Boards Students and through this practice paper they will learn about the various concepts associated with the Accessing columns/rows/single value from DataFrame easily.
I have already explained DataFrame creation practice problem in my previous practice paper DataFrame Practice Questions with Solutions (Part-I).
Question : How many ways you can access a column from a dataframe in Python? |
Answer : There are several ways to access a column from a DataFrame in Python:
|
Question : How many ways you can access a rows from a dataframe in Python? |
Answer : There are two main ways to access a row from a DataFrame in Python:
Both |
Question : How to access a single value in dataframe? |
Answer : To access a single value in a DataFrame in Python, we can use the at function or the iat function, depending on whether we want to access the value by label or by position.
|
Question : Given a DataFrame namely “employee” that stores the details of employees: |
a) Write a statement to display Name, Designation and Salary columns from the above employee DataFrame. |
b) Write a statement to display Name and Salary columns from the above DataFrame. |
c) Write a statement to display Bonus column only. |
d) Write a statement to display all the information from Employee ids ‘E102’ to ‘E104’ (Both are included) |
e) Write a statement to display all information of Employee id ‘E102’ and ‘E105’. |
f) Write a statement to display the employee’s name, designation and salary for those having employee ids as ‘E101′ and ‘E103’. |
g) Write a statement to display the employee’s name and bonus for those having employee id as ‘E101′ and ‘E103’. |
h) Write a statement to display the Salary of Darpan. |
Question : Given a DataFrame namely “data”. Write the code for the following commands |
a) Find all rows with the label “Pencil”. Extract all columns |
b) List products with count more than 25. |
c) List single True or False to signify if all prices are more than 100 or not. |
d) List 2nd , 3rd and 4th row. |
e) List only the columns Company and Price. |
f) List only rows with labels ‘Pencil’ and ‘Pen’ |
Question : Explain what the following statements are doing? df is the name of the dataframe. |
a) df.iloc[:5,] |
Answer : df.iloc[:5,] means that you are selecting the first 5 rows of the DataFrame df and all of the columns.The iloc attribute stands for “integer location” and is used to access a DataFrame using only integer positions for indexing. In this case, the colon : before the comma , indicates that we want to select all columns. If we wanted to select specific columns, we would replace the colon with a list of column indices.It’s worth noting that the trailing comma |
b) df.iloc[1:5] |
Answer : df.iloc[1:5] retrieves rows 1 through 4 (since Python is zero-indexed) from the dataframe named df, based on their integer location. The colon (:) in between 1 and 5 denotes a range, meaning it will select all rows between 1 (inclusive) and 5 (exclusive). |
c) df.iloc[5,0] |
Answer : The syntax df.iloc[5:0] would result in an empty slice, as the start index (5) is greater than the end index (0). It should be written as df.iloc[0:5] to select the first five rows of the dataframe. The iloc attribute is used to perform integer-based indexing on a dataframe, where rows and columns can be selected based on their numerical positions. |
d) df.iloc[1:5,0] |
Answer : The expression df.iloc[1:5,0] selects rows 1 through 4 (since Python indexing is zero-based, rows 1 to 5 are selected using 1:5 ) and column 0 (the first column) of the DataFrame df . It returns a Pandas Series object containing the values of the selected rows and column. |
e) df.iloc[1:5,:5] |
Answer : The code df.iloc[1:5,:5] in Python selects rows 1 to 4 (excluding the 5th row) and columns 0 to 4 (excluding the 5th column) from the dataframe df using integer-based indexing. |
f) df.iloc[2:7,1:3] |
Answer : The code df.iloc[2:7,1:3] selects rows 2 to 6 (index starts from 0, so 2 to 6 is a range of 5 rows) and columns 1 to 2 (index starts from 0, so 1 to 2 is a range of 2 columns) from the dataframe df using integer-based indexing. In other words, it returns a subset of the original dataframe containing the specified rows and columns. |
Question : Trying to extract the first five rows of DataFrame x, Aman has give code as: x.loc[0:5] but it is returning 6 rows. Why? Suggest the solution. |
Answer : The reason for the code x.loc[0:5] returning 6 rows is that the loc function includes both the start and end indices in the range. Therefore, when we pass 0:5, it will select the rows from index 0 to index 5, which includes 6 rows (0, 1, 2, 3, 4, 5).To extract the first 5 rows of a DataFrame, we should use x.loc[0:4] or x.iloc[0:5] . This will select the rows from index 0 to index 4, which includes the first five rows. |
Do you want to learn more and practice various types of questions?
Click on the below links:
- DataFrame Practice Questions with Solutions (Part-I) – for creation DataFrame
- DataFrame Practice Questions with Solutions (Part-3) – for add/delete rows/columns into DataFrame.
- DataFrame Practice Questions with Solutions (Part-4) – for DataFrame attributes and functions.
1 comment
Great explanation for every question. It helps a lot for preparation CBSE Board.