How to Slice Columns in Pandas DataFrame (With Examples)


You can use the following methods to slice the columns in a pandas DataFrame:

Method 1: Slice by Specific Column Names

df_new = df.loc[:, ['col1', 'col4']]

Method 2: Slice by Column Names in Range

df_new = df.loc[:, 'col1':'col4']

Method 3: Slice by Specific Column Index Positions

df_new = df.iloc[:, [0, 3]]

Method 4: Slice by Column Index Position Range

df_new = df.iloc[:, 0:3]

Note the subtle difference between loc and iloc in each of these methods:

  • loc selects rows and columns with specific labels
  • iloc selects rows and columns at specific integer positions

The following examples show how to use each method in practice with the following pandas DataFrame:

import pandas as pd

#create DataFrame with six columns
df = pd.DataFrame({'team': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],
                   'points': [18, 22, 19, 14, 14, 11, 20, 28],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12],
                   'steals': [4, 3, 3, 2, 5, 4, 3, 8],
                   'blocks': [1, 0, 0, 3, 2, 2, 1, 5]})

#view DataFrame
print(df)

  team  points  assists  rebounds  steals  blocks
0    A      18        5        11       4       1
1    B      22        7         8       3       0
2    C      19        7        10       3       0
3    D      14        9         6       2       3
4    E      14       12         6       5       2
5    F      11        9         5       4       2
6    G      20        9         9       3       1
7    H      28        4        12       8       5

Example 1: Slice by Specific Column Names

We can use the following syntax to create a new DataFrame that only contains the columns team and rebounds:

#slice columns team and rebounds
df_new = df.loc[:, ['team', 'rebounds']]

#view new DataFrame
print(df_new)

  team  rebounds
0    A        11
1    B         8
2    C        10
3    D         6
4    E         6
5    F         5
6    G         9
7    H        12

Example 2: Slice by Column Names in Range

We can use the following syntax to create a new DataFrame that only contains the columns in the range between team and rebounds:

#slice columns between team and rebounds
df_new = df.loc[:, 'team':'rebounds']

#view new DataFrame
print(df_new)

  team  points  assists  rebounds
0    A      18        5        11
1    B      22        7         8
2    C      19        7        10
3    D      14        9         6
4    E      14       12         6
5    F      11        9         5
6    G      20        9         9
7    H      28        4        12

Example 3: Slice by Specific Column Index Positions

We can use the following syntax to create a new DataFrame that only contains the columns in the index positions 0 and 3:

#slice columns in index positions 0 and 3
df_new = df.iloc[:, [0, 3]]

#view new DataFrame
print(df_new)

  team  rebounds
0    A        11
1    B         8
2    C        10
3    D         6
4    E         6
5    F         5
6    G         9
7    H        12

Example 4: Slice by Column Index Position Range

We can use the following syntax to create a new DataFrame that only contains the columns in the index position range between 0 and 3:

#slice columns in index position range between 0 and 3
df_new = df.iloc[:, 0:3]

#view new DataFrame
print(df_new)

  team  points  assists
0    A      18        5
1    B      22        7
2    C      19        7
3    D      14        9
4    E      14       12
5    F      11        9
6    G      20        9
7    H      28        4

Note: When using an index position range, the last index position in the range will not be included. For example, the rebounds column in index position 3 is not included in the new DataFrame.

Additional Resources

The following tutorials explain how to perform other common tasks in pandas:

How to Drop First Row in Pandas DataFrame
How to Drop First Column in Pandas DataFrame
How to Drop Duplicate Columns in Pandas

Leave a Reply

Your email address will not be published. Required fields are marked *