CBSE 2026 results are out, Mukul scored a perfect 100/100 in Computer ScienceSee all toppers →

KwickAcademy Python · 6 min · free

Pandas DataFrame: Create, Access and Modify

6 min5 KwickClipsFull text belowFree
Next lesson →Kajal Ma'am (MCA), teaching since 2004Remembered in this browser

Learn the pandas DataFrame in Python: create a table from dictionaries or Series, add, rename and drop data, and access it with loc and iloc.

On screen in this lesson

What is a DataFrame?

pandas: a library for tables of data
DataFrame: a table of rows and columns
Index: the labels of the rows
Column names: the labels of the columns
Each column is a Series

DataFrame attributes

AttributeTells youOur table
df.shaperows, columns(3, 2)
df.sizetotal values6
df.columnscolumn labelsEng, Sci
df.indexrow labelsOm, Anu, Raj
df.dtypestype per columnint64, int64
df.Tswaps rows, cols2 rows, 3 columns

Quick recap

DataFrame: a table with row and column labels
Create from dict of lists, Series, or list of dicts
Add with df[col] and loc; rename(); drop()
loc uses labels, iloc uses positions
shape, size, columns, index; iterrows()

Quick answers

What is a pandas DataFrame?

A table with rows and columns, with labels for both, like a class marks sheet.

What is the difference between loc and iloc?

loc uses labels and a loc slice includes the end label; iloc uses integer positions starting at zero.

KwickClips from this lesson

Short clips, one idea each. Good for revision the night before.

The full lesson, in text

Hello students, welcome to Kwickprep. Your class marks sheet has names down the side and subjects across the top. How can Python hold that whole table and answer questions about it? With a pandas DataFrame. Today we will create a DataFrame, access its values, and change its rows and columns.

Let us start with the new words. Pandas is a Python library for working with tables of data, and we load it with import pandas as p d. A DataFrame is a table with rows and columns, like your marks sheet. The index is the set of row labels, such as roll numbers or names. The column names are the labels across the top. Each single column is a Series, which is a list of values with labels.

The most common way is a dictionary of lists. A dictionary stores key and value pairs inside curly brackets. Each key becomes a column name, and each list becomes that column. P d dot DataFrame builds the table. We did not give row labels, so pandas numbers the rows zero, one and two.

We can also build a DataFrame from Series. Series e has English marks and Series s has Science marks. Each one has marks, with the names as its index. We put them in a dictionary, with Eng for English and Sci for Science as the keys. Pandas matches the rows by their labels, so Riya's marks stay in Riya's row. This time, the row labels are names, not numbers.

A third way is a list of dictionaries, where each dictionary is one row. Notice that Aman's dictionary has no marks. Pandas fills that gap with N a N, which means not a number, a missing value. Because of that missing value, the marks column becomes decimal, so eighty eight shows as eighty eight point zero.

To add a column, write the new column name in square brackets and give it a list. So the Grade column gets A and B. To add a row, use loc with a new row label. The loc tool picks rows by their labels, and we will see it again soon. Neha's row gets sixty four and C.

The rename method changes labels. We give it a dictionary of old name and new name, so Eng becomes Maths. To rename rows, use index instead of columns. The drop method deletes rows or columns. Here it deletes the row Anu and the column Sci. Both methods give back a new DataFrame, so we store it again in df.

Here we set the row labels by giving a list to df dot index. Now there are two ways to reach one value. Loc uses labels, so row Anu and column Sci gives sixty four. Iloc uses integer positions, which start at zero. Row one, column one is also Anu's Science mark. So both lines print sixty four. Find the error: loc of one, Sci gives a key error here, because one is not a row label.

A slice picks a range of rows, written with a colon. Pause and predict. How many rows does loc give from Om to Anu? The answer is two, because a loc slice includes the end label. Iloc from zero to one gives only one row. An iloc slice stops before the end position, just like a list.

Boolean indexing picks rows using a condition. The greater than sign asks if a value is bigger. So df of Eng, greater than seventy, gives True or False for each row. Putting that inside square brackets keeps only the True rows. Raj scored sixty four, so Raj's row is left out.

Attributes are facts about the DataFrame, written after a dot with no brackets. Shape gives rows and columns, so three and two. Size gives the total number of values, six. Columns gives the column labels. Index gives the row labels. D types gives the data type of each column. T swaps rows with columns, which is called the transpose.

To go through a DataFrame one row at a time, use iterrows. Each turn of the loop gives two things. Name is the row label, and row is that row as a Series. So row of Eng plus row of Sci gives each student's total. Om's total is one hundred seventy nine.

Let us revise what we learned today. A DataFrame is a table with labels for rows and columns. You can create it from a dictionary of lists, from Series, or from a list of dictionaries. Add columns with square brackets and rows with loc, then use rename and drop to change them. Loc uses labels, while iloc uses positions. Attributes like shape describe the table, and iterrows goes through each row.

Courses that teach this

CourseUnit
CBSE Class 12 Informatics Practices (065)Data Handling using Pandas -I
CBSE Class 11 Artificial Intelligence (843)Python Programming
CBSE Class 12 Artificial Intelligence (843)Python Programming - II (evaluated in practicals)
Programming All levels PythonWorking with Data (Intro Libraries)

Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.

Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

Want a plan that actually fits your board dates?

Ask Kajal Ma'am directly, 20+ years teaching computer science. Free demo class first, no payment.

Talk to Kajal Ma'am on WhatsApp

Or see the Class 12 Computer Science course →

Studying outside India?

We coach CBSE, IGCSE & international students across the globe, one-to-one, in your local time zone.

Visit International →