0% found this document useful (0 votes)
11 views

Dataframe

This document is a practice paper designed for Class XII CBSE Board students, focusing on accessing DataFrames in Python. It provides various questions and solutions related to accessing columns, rows, and single values from DataFrames, along with explanations of different methods like loc and iloc. The paper aims to enhance students' understanding of DataFrame manipulation through practical examples and coding exercises.

Uploaded by

Deep Malaviya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Dataframe

This document is a practice paper designed for Class XII CBSE Board students, focusing on accessing DataFrames in Python. It provides various questions and solutions related to accessing columns, rows, and single values from DataFrames, along with explanations of different methods like loc and iloc. The paper aims to enhance students' understanding of DataFrame manipulation through practical examples and coding exercises.

Uploaded by

Deep Malaviya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Home » DataFrame Practice Questions with Solutions


(Part-2 Accessing DataFrame)

Article Informatics Practices Assignments

DATAFRAME PRACTICE QUESTIONS WITH


SOLUTIONS (PART-2 ACCESSING
DATAFRAME)
Class XII (CBSE Board)
written by Mahesh Verma February 17, 2023

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 f rom a dataf rame in Python?

Answer : T here are several ways to access a

column from a DataFrame in Python:

1. Using the column name:

We can access a column by its name using


indexing notation. For example,
df['column_name'] will return the column
with the name ‘column_name’.

2. Using the dot notation:

If the column name does not contain any


spaces or special characters, we can access
the column using the dot notation. For
example, df.column_name will return the
column with the name ‘column_name’.

3. Using the iloc f unction:

We can access a column by its index using


the iloc function. For example, df.iloc[:,
1] will return the second column of the
DataFrame.

4. Using the loc f unction:

We can access a column by its name using


the loc function. For example, df.loc[:,
'column_name'] will return the column with
the name ‘column_name’.

Question : How many ways you can access a


rows f rom a dataf rame in Python?

Answer : T here are two main ways to access a


row from a DataFrame in Python:

1. Using the loc f unction: T he loc function is

used to access rows by label. We can pass


the label of the row as an argument to loc .

For example, df.loc[3] will return the row

with label 3.

2. Using the iloc f unction: T he iloc function

is used to access rows by index. We can pass

the index of the row as an argument to


iloc . For example, df.iloc[3] will return the

row at index 3.

Both loc and iloc can also be used to access a


range of rows by passing a slice object. For
example, df.loc[3:5] will return a DataFrame
with rows from label 3 to label 5, and
df.iloc[3:6] will return a DataFrame with rows
from index 3 to index 5.

Question : How to access a single value in


dataf rame?

Answer : T o 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.

T he at f unction is used to access a single


value by label. We can pass the row label and
the column label as arguments to at . For
example, df.at[3, 'B'] will return the value
in the cell at row label 3 and column label ‘B’.

T he iat f unction is used to access a single


value by position. We can pass the row index
and the column index as arguments to iat .
For example, df.iat[3, 1] will return the
value in the cell at row index 3 and column
index 1.

Question : Given a DataFrame namely

“employee” that stores the details of


employees:

a) Write a statement to display Name,


Designation and Salary columns f rom the

above employee DataFrame.

b) Write a statement to display Name and


Salary columns f rom the above DataFrame.

c) Write a statement to display Bonus

column only.

d) Write a statement to display all the

inf ormation f rom Employee ids ‘E102’ to


‘E104’ (Both are included)

e) Write a statement to display all

inf ormation of Employee id ‘E102’ and ‘E105’.

f ) Write a statement to display the


employee’s name, designation and salary

f or those having employee ids as ‘E101′ and


‘E103’.

g) Write a statement to display the

employee’s name and bonus f or 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 f or the f ollowing

commands

a) Find all rows with the label “Pencil”.

Extract all columns

b) List products with count more than 25.

c) List single T rue or False to signif y if all

prices are more than 100 or not.

d) List 2 nd , 3 rd and 4th row.

e) List only the columns Company and Price.

f ) List only rows with labels ‘Pencil’ and ‘Pen’

Question : Explain what the f ollowing

statements are doing? df is the name of the

dataf rame.

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.T he 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 , is


optional and can be omitted without changing
the behavior of the code.

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. T he 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 : T he 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. T he 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 : T he 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 : T he 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 : T he 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 : T rying to extract the f irst f ive

rows of DataFrame x, Aman has give code

as:

x.loc[0:5]

but it is returning 6 rows. Why? Suggest the


solution.

Answer : T he 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. T herefore, 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).T o extract the first 5 rows


of a DataFrame, we should use x.loc[0:4] or

x.iloc[0:5] . T his 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) – f or creation DataFrame

DataFrame Practice Questions with Solutions (Part-


3) – f or add/delete rows/columns into
DataFrame.

DataFrame Practice Questions with Solutions (Part-


4) – f or DataFrame attributes and f unctions.

ACCESSING COLUMNS FROM DATAFRAME

ACCESSING ROWS FROM DATAFRAME

ACCESSING SINGLE VALUE FROM DATAFRAME

ALL() METHOD AT AND IAT IN DATAFRAME CBSE

CLASS 12 IP 065 CLASS-12 IP CLASS-XII

IMPORTANT QUESTIONS ON DATAFRAME

INFORMATION PRACTICES LOC AND ILOC IN DATAFRAME

 1 comment
1    

MAHESH VERMA

I have been working for 10 years in


software developing field. I have
designed and developed applications using C#, SQL
Server, Web API, AngularJS, Angular, React, Python
etc. I love to do work with Python, Machine Learning
and to learn all the new technologies also with the
expertise to grasp new concepts quickly and utilize
the same in a productive manner.

previous post
DATAFRAME PRACTICE QUESTIONS WITH
SOLUTIONS (PART-I DATAFRAME CREATION)

next post
SELF STUDY IS THE BEST STUDY

YOU MAY ALSO LIKE

TOP 25 + REACT 19 PROPS INTERVIEW


QUESTIONS...
January 17, 2025

1 COMMENT

NAMAN REPLY

 February 27, 2023 - 5:19 pm

Great explanation for every question. It helps a lot


for preparation CBSE Board.

LEAVE A COMMENT

Your Comment

Name*

Email*

Website

Save my name, email, and website in this browser


for the next time I comment.

SUBMIT

Search

SEARCH

Recent Posts

Top 25 + React 19 Props Interview Questions and


Answers

Word Chain Game in React19

Informatics Practices Question Paper 2023

Python Previous Year Paper

Create React 19 App

CATEGORIES

 Array Programs (14)

 Article (96)

 Artificial Intelligence (1)

 C-Language (2)

 C++ Coding Challenges (2)

 C++ Language (3)

 C++ Programs (26)

 CBSE News (1)

 Computer Networks and Internet (5)

 Data Science (3)

 Data Structure (1)

 Informatics Practices Assignments (9)

 JAVA Programming (20)

 Java Programs (15)

 JavaScript (1)

 Kindergarten (2)

 Machine Learning (13)

 Node (6)

 PHP (3)

 Previous Year Questions Papers (22)

 Programming Challenges (3)

 Programs (44)

 Python (17)

 Python Programming Challenges (1)

 Python Programs (1)

 React (6)

 Recursive Function (14)

 School (39)

 Uncategorized (8)

 Your Ultimate Student Guide: Navigating K-12 to


College (25)

FOLLOW US FOR LATEST UPDATE

    
START A CONVERSATION

 India: +91 9999-6101-75

[email protected]

IMPORTANT PAGES

Home

Courses

Articles

Books

Online T utor

Sample Papers

SCHOOL & UNIVERSITIES

CBSE Board

CISCE

CBSE

IB

Other Board

International Universities

OUR POLICIES

T erm & Condition

Privacy Policy

FAQ

Support

Services

COURSES

C-Language Data Structure

C++ BCA

Class 12th Btech

Class 11th JAVA

Python .NET

MCA Angular

B.Sc (IT ) React

Vue T K Inter

MySQL

SQL

RECENT POSTS

T op 25 + React 19 Props Interview


Questions and Answers

Word Chain Game in React19

Inf ormatics Practices Question


Paper 2023
© Study Trigger. All Rights Reserved

You might also like