Informatics Practices

Pandas DataFrame Operations | Selecting, Adding, Deleting Rows & Columns | CBSE Class 12 Informatics Practices (2026–27)

Class 12 · Informatics Practices

Operations on Pandas DataFrame

Once a DataFrame has been created, Pandas provides several operations to view, access, modify, and analyze the data. These operations help users work efficiently with rows and columns in a table.


Sample DataFrame

import pandas as pd

data = {
    "Name": ["Amit", "Neha", "Rohan", "Priya"],
    "Marks": [85, 92, 78, 88],
    "City": ["Jaipur", "Delhi", "Mumbai", "Jaipur"]
}

df = pd.DataFrame(data)

print(df)
Output
    Name  Marks     City
0   Amit     85   Jaipur
1   Neha     92    Delhi
2  Rohan     78   Mumbai
3  Priya     88   Jaipur

Selecting a Single Column

A column can be selected using its column name.

Syntax

df["Column_Name"]

Example

print(df["Marks"])
Output
0    85
1    92
2    78
3    88
Name: Marks, dtype: int64

Selecting Multiple Columns

print(df[["Name","Marks"]])
Output
    Name  Marks
0   Amit     85
1   Neha     92
2  Rohan     78
3  Priya     88

Adding a New Column

A new column can be added by assigning values to a new column name.

df["Grade"] = ["B","A","C","B"]

print(df)

Updating an Existing Column

df["Marks"] = df["Marks"] + 5

print(df)

Every student's marks increase by 5.


Deleting a Column

The drop() method removes a column from a DataFrame.

Syntax

df.drop("Column_Name", axis=1, inplace=True)

Example

df.drop("Grade", axis=1, inplace=True)

  • axis=1 indicates a column.
  • inplace=True permanently updates the DataFrame.


Renaming Columns

The rename() method changes column names.

df.rename(
    columns={"Marks":"Percentage"},
    inplace=True
)

print(df)

head() Function

Displays the first five rows by default.

df.head()

First 2 Rows

df.head(2)

tail() Function

Displays the last five rows by default.

df.tail()

Last 2 Rows

df.tail(2)

Iterating Through a DataFrame

Iteration means accessing rows one by one.

Using iterrows()

for index, row in df.iterrows():
    print(index, row["Name"], row["Marks"])
Output
0 Amit 85
1 Neha 92
2 Rohan 78
3 Priya 88

Label Indexing using loc[]

The loc[] method selects data using row labels and column names.

Syntax

df.loc[row_label, column_label]

Example 1

print(df.loc[1])

Displays the complete second row.


Example 2

print(df.loc[1,"Name"])
Output
Neha

Select Multiple Rows

print(df.loc[1:3])

Selecting Specific Rows and Columns

print(df.loc[0:2, ["Name","Marks"]])

Boolean Indexing

Boolean indexing filters rows based on a condition.

Syntax

df[condition]

Example

print(df[df["Marks"] > 80])
Output
    Name  Marks     City
0   Amit     85   Jaipur
1   Neha     92    Delhi
3  Priya     88   Jaipur

Multiple Conditions

print(
df[
(df["Marks"]>80)
&
(df["City"]=="Jaipur")
]
)
Output
    Name  Marks    City
0   Amit     85  Jaipur
3  Priya     88  Jaipur

Common DataFrame Operations

Operation Syntax
Select Column df["Marks"]
Multiple Columns df[["Name","Marks"]]
Add Column df["Grade"]=...
Delete Column df.drop(...)
Rename Column df.rename()
First Rows df.head()
Last Rows df.tail()
Label Indexing df.loc[]
Boolean Indexing df[df["Marks"]>80]

Common Errors

Error Reason
KeyError Incorrect column name.
AttributeError Using an incorrect function name.
SyntaxError Missing brackets or quotes.
ValueError Incorrect number of values while adding a column.

Quick Revision

Function Purpose
head() Shows first 5 rows
tail() Shows last 5 rows
loc[] Label-based indexing
drop() Delete column
rename() Rename column
iterrows() Iterate through rows
Boolean Indexing Filter rows using conditions

CBSE Exam Tips

  • Remember that loc[] uses row labels and column names.
  • Use axis=1 when deleting a column with drop().
  • Practice Boolean Indexing questions carefully.
  • Learn the syntax of rename() and iterrows().
  • Be able to predict the output of DataFrame programs.

Summary

Pandas DataFrame operations make it easy to select, add, update, delete, and filter data. Functions such as head(), tail(), loc[], and Boolean Indexing are widely used for analyzing datasets. These operations are essential for both the CBSE practical examination and real-world data analysis.