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
  • 1
  • 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
  • 729,927
  • 78
  • 1,141
  • 1,090
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
  • 691
  • 5
  • 23
0

try this

df.iloc[::,0:3]

result should be the part you want

Yang
  • 140
  • 1
  • 7