Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
60 views4 pages

Slicing Pandas Dataframe - GeeksforGeeks

The document provides a guide on slicing a Pandas DataFrame using integer-based indexing with iloc[] and label-based indexing with loc[]. It includes examples of creating a custom DataFrame, selecting rows and columns, and using Boolean conditions for filtering. The document emphasizes the importance of understanding both methods for effective data manipulation in Pandas.

Uploaded by

Sukhwinder Singh
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)
60 views4 pages

Slicing Pandas Dataframe - GeeksforGeeks

The document provides a guide on slicing a Pandas DataFrame using integer-based indexing with iloc[] and label-based indexing with loc[]. It includes examples of creating a custom DataFrame, selecting rows and columns, and using Boolean conditions for filtering. The document emphasizes the importance of understanding both methods for effective data manipulation in Pandas.

Uploaded by

Sukhwinder Singh
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/ 4

22/04/2025, 11:59 Slicing Pandas Dataframe | GeeksforGeeks

Search...

Slicing Pandas Dataframe


Last Updated : 17 Mar, 2025

Slicing a Pandas DataFrame is a important skill for extracting specific


data subsets. Whether you want to select rows, columns or individual
cells, Pandas provides efficient methods like iloc[] and loc[]. In this
guide we’ll explore how to use integer-based and label-based indexing
to slice DataFrames effectively.

Create a Custom Dataframe


Let’s import pandas library and create pandas dataframe from custom
nested list.

import pandas as pd

player_list = [['M.S.Dhoni', 36, 75, 5428000],


['A.B.D Villers', 38, 74, 3428000],
['V.Kohli', 31, 70, 8428000],
['S.Smith', 34, 80, 4428000],
['C.Gayle', 40, 100, 4528000],
['J.Root', 33, 72, 7028000],
['K.Peterson', 42, 85, 2528000]]

df = pd.DataFrame(player_list, columns=['Name', 'Age', 'Weight',


'Salary'])
print(df)

Output:

https://www.geeksforgeeks.org/slicing-indexing-manipulating-and-cleaning-pandas-dataframe/ 1/9
22/04/2025, 11:59 Slicing Pandas Dataframe | GeeksforGeeks

Slicing Using iloc[] (Integer-Based Indexing)


The iloc[] method in Pandas allows us to extract specific rows and
columns based on their integer positions starting from 0. Each number
represents a position in the DataFrame not the actual label of the row or
column.

DataSlicing Rows
Science IBM in dataframe
Certification Data preprocessing Data Manipulation Data Analysi Sign In

Row slicing means selecting a specific set of rows from the DataFrame
while keeping all columns.

df1 = df.iloc[0:4]
print(df1)

Output:

Slicing Columns in dataframe

Column slicing means selecting a specific set of columns from the


DataFrame while keeping all rows.
https://www.geeksforgeeks.org/slicing-indexing-manipulating-and-cleaning-pandas-dataframe/ 2/9
22/04/2025, 11:59 Slicing Pandas Dataframe | GeeksforGeeks

df1 = df.iloc[:, 0:2]

print(df1)

Output:

Selecting a Specific Cell in a Pandas DataFrame

If you need a single value from a DataFrame you can specify the exact
row and column position

value = df.iloc[2, 3]
print("Specific Cell Value:", value)

Output:

Specific Cell Value: 8428000

Using Boolean Conditions in a Pandas DataFrame

Instead of selecting rows by index we can use Boolean conditions (e.g.,


Age > 35) to filter rows dynamically.

data = df[df['Age'] > 35].iloc[:, :]


print("\nFiltered Data based on Age > 35:\n", data)

Output:

https://www.geeksforgeeks.org/slicing-indexing-manipulating-and-cleaning-pandas-dataframe/ 3/9
22/04/2025, 11:59 Slicing Pandas Dataframe | GeeksforGeeks

Slicing Using loc[]


We can also implement slicing using the loc function in Pandas but
there are some limitations to be aware of. The loc function relies on
labels meaning that if your DataFrame has custom labels instead of
default integer indices you need to be careful with how you specify
them.

Slicing Rows in Dataframe

With loc[] we can extract a range of rows by their labels instead of


integer positions.

df.set_index('Name', inplace=True)

custom = df.loc['A.B.D Villers':'S.Smith']


print(custom)

Output:

Selecting Specified cell in Dataframe

loc[] allows us to fetch a specific value based on row and column labels

value = df.loc['V.Kohli', 'Salary']


print("\nValue of the Specific Cell (V.Kohli, Salary):", value)

Output:

Value of the Specific Cell (V.Kohli, Salary): 8428000

https://www.geeksforgeeks.org/slicing-indexing-manipulating-and-cleaning-pandas-dataframe/ 4/9

You might also like