The slice()
method in Pandas is utilized with the loc[] property to select a range of rows or columns by their labels or index.
Example
import pandas as pd
# create a DataFrame
df = pd.DataFrame({
'A': [1, 2, 3, 4],
'B': [5, 6, 7, 8],
'C': [9, 10, 11, 12]
})
# select rows from index 0 to index 2 (inclusive)
result = df.loc[slice(0, 2)]
print(result)
'''
Output
A B C
0 1 5 9
1 2 6 10
2 3 7 11
'''
slice() Syntax
The syntax of the slice()
method in Pandas is:
slice(start, stop, step)
slice() Arguments
The slice()
method takes following arguments:
start
(optional) - the starting index or labelstop
(optional) - the stopping index or label. This endpoint is inclusive in Pandasstep
(optional) - the step size for selecting every nth row/column.
slice() Return Value
The slice()
method creates a slice object that is used with .loc[]
to return a selected subset of a DataFrame.
Example 1: Use slice() to Select Rows
import pandas as pd
# create a DataFrame
df = pd.DataFrame({'A': [1, 2, 3, 4],
'B': [5, 6, 7, 8]})
# create a slice object for rows
row_slice = slice(1, 3)
# use the slice object with .loc to select a subset of the DataFrame
subset_df = df.loc[row_slice]
print(subset_df)
Output
A B 1 2 6 2 3 7 3 4 8
Here, when we use slice(1, 3)
with the .loc
accessor on the df DataFrame, it will select the rows starting at index 1 and up to and including index 3.
Example 2: Use slice() With Row Labels
import pandas as pd
df = pd.DataFrame({
'A': [1, 2, 3, 4],
'B': [5, 6, 7, 8]
}, index=['w', 'x', 'y', 'z'])
# use slice() to select rows labeled from 'x' to 'y'
result = df.loc[slice('x','y'), :]
print(result)
Output
A B x 2 6 y 3 7
Here, we have first created the df DataFrame with columns A
and B
. The df is indexed with labels w
, x
, y
, and z
.
We used the .loc[]
accessor with slice('x', 'y')
to select rows. The slice()
method here defines the range of the index labels.
The slicing starts with label 'x'
and ends with label 'y'
(inclusive). The :
in the row position indicates that we want to select all rows.
Example 3: Use slice() With Column Labels
import pandas as pd
df = pd.DataFrame({
'A': [1, 2, 3, 4],
'B': [5, 6, 7, 8],
'C': [9, 10, 11, 12],
'D': [13, 14, 15, 16]
}, index=['w', 'x', 'y', 'z'])
# use slice() to select columns from 'A' to 'C'
result = df.loc[:, slice('A', 'C')]
print(result)
Output
A B C w 1 5 9 x 2 6 10 y 3 7 11 z 4 8 12
In the above example, we are using slice()
within .loc[]
to select a range of columns.
Here, slice('A', 'C')
means we start from column A
and go up to and including column C
.
The :
in the row position indicates that we want to select all rows.
Example 4: Using slice() With a Specific Step
import pandas as pd
df = pd.DataFrame({
'A': [1, 2, 3, 4],
'B': [5, 6, 7, 8],
'C': [9, 10, 11, 12],
'D': [13, 14, 15, 16],
'E': [17, 18, 19, 20]
}, index=['w', 'x', 'y', 'z'])
# select every second column from 'A' to 'E'
result_columns = df.loc[:, slice('A', 'E', 2)]
# select every second row starting from 'w' to 'z'
result_rows = df.loc[slice('w', 'z', 2), :]
print("Selected Columns with Step:\n", result_columns)
print("\nSelected Rows with Step:\n", result_rows)
Output
Selected Columns with Step: A C E w 1 9 17 x 2 10 18 y 3 11 19 z 4 12 20 Selected Rows with Step: A B C D E w 1 5 9 13 17 y 3 7 11 15 19
Here,
- result_columns is obtained by selecting every second column from
A
toE
, using a step of 2. This includes columnsA
,C
, andE
. - result_rows is obtained by selecting every second row from the start of the DataFrame to the end (
w
toz
), also with a step of 2. This includes rowsw
andy
.