I want to drop a range of rows and columns of a dataframe, I did it as follow:

df.drop(df.columns[3:], axis=1, inplace=True)
df.drop(df.index[3:], axis=0, inplace=True)

Can I do the two processes in one method instead of two? Or is there any more sufficient way to accomplish this?


1 Answer 1


Why don't you do this:

df = df.iloc[:3, :3]

It returns the same thing as your code.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.