0

How can I delete column on index 2 onwards for a dataframe that contains 10 columns. The dataframe looks like this:

column1    column2    column3    column4 ...

The task is to delete column3-column10

Prog101
  • 109
  • 2
  • 10

3 Answers3

2

Invert logic - select first 2 columns by positions by DataFrame.iloc:

df = df.iloc[:, :2]

If need DataFrame.drop select columns names with indexing:

df = df.drop(df.columns[2:], axis=1)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
0

This should work for you

df.drop(columns=df.columns[2:])

df.drop(columns=[...]) will drop the provided columns

df.columns[2:] will return the list of columns, [2:] selects all columns, starting from the third column, all the way to the end.

Bobs Burgers
  • 761
  • 1
  • 5
  • 26
0

try this

df.iloc[::,0:3]

result should be the part you want

Yang
  • 218
  • 3
  • 9