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
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)
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.