24

I want to select all but the 3 last columns of my dataframe.

I tried :

df.loc[:,-3]

But it does not work

Edit : title

Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
Vincent
  • 1,534
  • 3
  • 20
  • 42

2 Answers2

40

Select everything EXCEPT the last 3 columns, do this using iloc:

In [1639]: df
Out[1639]: 
   a  b  c  d  e
0  1  3  2  2  2
1  2  4  1  1  1

In [1640]: df.iloc[:,:-3]
Out[1640]: 
   a  b
0  1  3
1  2  4
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
9

Use this df.columns being sliced, and putted into a df[...] bracket:

print(df[df.columns[:-3]])
U13-Forward
  • 69,221
  • 14
  • 89
  • 114