logo_learn_stats

The right way to Slice Columns in Pandas DataFrame (With Examples)

Posted on
banner 336x280

You’ll be able to worth please see modes to slice the columns in a pandas DataFrame:

Mode 1: Slice by means of Explicit Column Names

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

Mode 2: Slice by means of Column Names in Territory

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

Mode 3: Slice by means of Explicit Column Index Positions

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

Mode 4: Slice by means of Column Index Place Territory

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

Be aware the sly excess between loc and iloc in every of those modes:

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

Refer to examples display the best way to worth every mode in follow with please see pandas DataFrame:

import pandas as pd

#develop DataFrame with six columns
df = pd.DataFrame({'workforce': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],
                   'issues': [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)

  workforce  issues  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

Instance 1: Slice by means of Explicit Column Names

We will worth please see syntax to develop a pristine DataFrame that handiest comprises the columns workforce and rebounds:

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

#view pristine DataFrame
print(df_new)

  workforce  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

Instance 2: Slice by means of Column Names in Territory

We will worth please see syntax to develop a pristine DataFrame that handiest comprises the columns within the territory between workforce and rebounds:

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

#view pristine DataFrame
print(df_new)

  workforce  issues  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

Instance 3: Slice by means of Explicit Column Index Positions

We will worth please see syntax to develop a pristine DataFrame that handiest comprises the columns within the index positions 0 and 3:

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

#view pristine DataFrame
print(df_new)

  workforce  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

Instance 4: Slice by means of Column Index Place Territory

We will worth please see syntax to develop a pristine DataFrame that handiest comprises the columns within the index place territory between 0 and 3:

#slice columns in index place territory between 0 and three
df_new = df.iloc[:, 0:3]

#view pristine DataFrame
print(df_new)

  workforce  issues  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

Be aware: When the use of an index place territory, the endmost index place within the territory is probably not integrated. For instance, the rebounds column in index place 3 isn’t integrated within the pristine DataFrame.

Backup Assets

Refer to tutorials provide an explanation for the best way to carry out alternative usual duties in pandas:

The right way to Leave First Row in Pandas DataFrame
The right way to Leave First Column in Pandas DataFrame
The right way to Leave Replica Columns in Pandas

banner 336x280

Leave a Reply

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