VOOZH about

URL: https://www.geeksforgeeks.org/pandas/how-to-take-column-slices-of-dataframe-in-pandas/

⇱ How to take column-slices of DataFrame in Pandas? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to take column-slices of DataFrame in Pandas?

Last Updated : 23 Jul, 2025

In this article, we will learn how to slice a DataFrame column-wise in Python. DataFrame is a two-dimensional tabular data structure with labeled axes. i.e. columns.

Creating Dataframe to slice columns

Output:

πŸ‘ Image

Method 1: Slice Columns in pandas using reindex

Slicing column from 'c' to 'b'.

Output:

πŸ‘ Image

Method 2: Slice Columns in pandas using loc[]

The df.loc[] is present in the Pandas package loc can be used to slice a Dataframe using indexing. Pandas DataFrame.loc attribute accesses a group of rows and columns by label(s) or a boolean array in the given DataFrame.

Syntax: [ : , first : last : step]

Example 1:

Slicing column from 'b' to 'd' with step 2.

Output:

πŸ‘ Image

Example 2:

Slicing column from 'c' to 'e' with step 1.

Output:

πŸ‘ Image

Method 3: Slice Columns in pandas using iloc[]

The iloc is present in the Pandas package. The iloc can be used to slice a Dataframe using indexing. df.iloc[] method is used when the index label of a data frame is something other than numeric series of 0, 1, 2, 3….n or in case the user doesn’t know the index label. Rows can be extracted using an imaginary index position that isn’t visible in the data frame.

Syntax: [ start : stop : step]

Example 1:

Slicing column from '1' to '3' with step 1.

Output:

πŸ‘ Image

Example 2:

Slicing column from '0' to '3' with step 2.

Output:

πŸ‘ Screenshot-2024-07-09-160446
Output


Comment

Explore