VOOZH about

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

⇱ Slicing Pandas Dataframe - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Slicing Pandas Dataframe

Last Updated : 14 Apr, 2026

Slicing a Pandas DataFrame is an important skill for extracting specific data subsets. Whether selecting rows, columns or individual cells, Pandas provides efficient methods such as iloc[]and loc[]. It focuses on using 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.

Output:

👁 creating_custom_dataframe

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.

Slicing Rows in dataframe

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

Output:

👁 slicing_using_iloc

Slicing Columns in dataframe

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

Output:

👁 slicing_columns

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

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.

Output:

👁 Screenshot-2025-03-15-095646

Slicing Using loc[]

Slicing can also be performed using the loc[] function in Pandas. Since loc[] is label-based, it selects data using row and column labels. As a result, when working with custom indices, it is important to reference the correct labels during selection.

Slicing Rows in Dataframe

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

Output:

👁 slicing_using_loc

Selecting Specified cell in Dataframe

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

Output:

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

Comment

Explore