3

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?

Fatemeh Asgarinejad
  • 1,198
  • 1
  • 10
  • 18
Mohamed Abduljawad
  • 265
  • 2
  • 4
  • 13

1 Answers1

5

Why don't you do this:

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

It returns the same thing as your code.

Fatemeh Asgarinejad
  • 1,198
  • 1
  • 10
  • 18