20

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
  • 31,737
  • 7
  • 30
  • 50
Vincent
  • 1,356
  • 3
  • 19
  • 41

2 Answers2

35

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
  • 31,737
  • 7
  • 30
  • 50
8

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

print(df[df.columns[:-3]])
U12-Forward
  • 65,118
  • 12
  • 70
  • 89