Slicing Data
Slicing Data in Pandas
Slicing allows you to extract specific portions of a DataFrame or Series based on index positions or labels.
Slicing Rows Using Indexing
You can slice rows using the .loc[]
or .iloc[]
methods.
import pandas as pd
# Sample DataFrame
data = {
"Name": ["Jasmeet", "Bob", "Charlie", "David", "Emma"],
"Age": [25, 30, 35, 40, 29],
"Salary": [50000, 60000, 55000, 70000, 65000]
}
df = pd.DataFrame(data)
print(df)
- Slicing Rows Using
.iloc[]
(Position-Based Indexing) Use.iloc[start:stop]
to select rows by index position.
# Select First 3 Rows
sliced_df = df.iloc[0:3]
print(sliced_df)
Output
Name Age Salary
0 Jasmeet 25 50000
1 Bob 30 60000
2 Charlie 35 55000
Note: iloc[0:3]
selects rows 0, 1, and 2, but not row 3 (stop index is excluded).
- Slicing Rows Using
.loc[]
(Label-Based Indexing) Use.loc[start_label:end_label]
to select rows by label (index name).
# Select Rows from Index 1 to 3 (Inclusive)
sliced_df = df.loc[1:3]
print(sliced_df)
Output
Name Age Salary
1 Bob 30 60000
2 Charlie 35 55000
3 David 40 70000
- Note:
.loc[]
includes the stop index, unlike.iloc[]
.
Slicing Columns
You can also slice specific columns.
# Select 'Name' and 'Age' Columns
sliced_df = df.loc[:, ["Name", "Age"]]
print(sliced_df)
Output
Name Age
0 Jasmeet 25
1 Bob 30
2 Charlie 35
3 David 40
4 Emma 29
:
means select all rows, and["Name", "Age"]
selects specific columns.
Slicing a Specific Range of Rows and Columns
# Select Rows 1 to 3 and Columns 'Name' and 'Age'
sliced_df = df.loc[1:3, ["Name", "Age"]]
print(sliced_df)
Output
Name Age
1 Bob 30
2 Charlie 35
3 David 40
Slicing Using Conditions
You can slice data based on conditions.
# Select Rows Where Age > 30
sliced_df = df[df["Age"] > 30]
print(sliced_df)
Output
Name Age Salary
2 Charlie 35 55000
3 David 40 70000
Selecting Alternate Rows
Use step indexing (::step
) to select rows at intervals.
# Select Every 2nd Row
sliced_df = df.iloc[::2]
print(sliced_df)
Output
Name Age Salary
0 Jasmeet 25 50000
2 Charlie 35 55000
4 Emma 29 65000
::2
selects every second row (skipping one in between).
Slicing Using Negative Indexing
You can use negative indexing to select rows from the end.
# Select Last 2 Rows
sliced_df = df.iloc[-2:]
print(sliced_df)
Output
Name Age Salary
3 David 40 70000
4 Emma 29 65000
-2:
selects the last two rows.
Slicing Methods
Method | Description |
---|---|
df.iloc[0:3] | Select rows by position (excluding stop index). |
df.loc[1:3] | Select rows by index label (including stop index). |
df.loc[:, ["col1", "col2"]] | Select specific columns. |
df.loc[1:3, ["col1", "col2"]] | Select rows and columns together. |
df[df["col"] > value] | Filter rows based on conditions. |
df.iloc[::2] | Select every 2nd row. |
df.iloc[-2:] | Select the last 2 rows. |
No questions available.