Skip to main content
added 90 characters in body
Source Link
Mykola Zotko
  • 18.3k
  • 7
  • 89
  • 91

To exclude some columns you can dropdrop them in the column index. For example:

   A   B    C     D
0  1  10  100  1000
1  2  20  200  2000

Select all columns except onetwo:

df[df.columns.drop('C'['B', 'D'])]

Output:

   A   B     DC
0  1  10  1000100
1  2  20  2000200

Select all except twoYou can also use the method truncate to select middle columns:

df[df.columnsdf.droptruncate(['B'before='B', 'D']after='C', axis=1)]

Output:

   A B    C
0  110  100
1  220  200

To exclude some columns you can drop them in the column index. For example:

   A   B    C     D
0  1  10  100  1000
1  2  20  200  2000

Select all columns except one:

df[df.columns.drop('C')]

Output:

   A   B     D
0  1  10  1000
1  2  20  2000

Select all except two:

df[df.columns.drop(['B', 'D'])]

Output:

   A    C
0  1  100
1  2  200

To exclude some columns you can drop them in the column index. For example:

   A   B    C     D
0  1  10  100  1000
1  2  20  200  2000

Select all except two:

df[df.columns.drop(['B', 'D'])]

Output:

   A    C
0  1  100
1  2  200

You can also use the method truncate to select middle columns:

df.truncate(before='B', after='C', axis=1)

Output:

    B    C
0  10  100
1  20  200
Source Link
Mykola Zotko
  • 18.3k
  • 7
  • 89
  • 91

To exclude some columns you can drop them in the column index. For example:

   A   B    C     D
0  1  10  100  1000
1  2  20  200  2000

Select all columns except one:

df[df.columns.drop('C')]

Output:

   A   B     D
0  1  10  1000
1  2  20  2000

Select all except two:

df[df.columns.drop(['B', 'D'])]

Output:

   A    C
0  1  100
1  2  200