Slicing Pandas Dataframe
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:

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.
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.
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:

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
In summary both iloc[] and loc[] provide versatile slicing capabilities in Pandas. While iloc[] is integer-based loc[] relies on labels requiring careful consideration when working with custom indices or mixed data types.