2
$\begingroup$

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?

$\endgroup$
4
$\begingroup$

Why don't you do this:

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

It returns the same thing as your code.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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