-1

I want to delete the first column in my DataFrame, and I found the answer on the Internet. The answer is to delete columns 1, 2 and 4. I want to know why this line of code can delete columns and what is the role of axis here?

df = df.drop(df.columns[[0, 1, 3]], axis=1)
Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52
Yue
  • 1

1 Answers1

1

your example works fine.

df.drop(df.columns[[0, 1, 3]], axis=1)

sometimes you'll see it like this:

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

Another way to accomplish this would be by typing the names of the column headers:

df.drop(['column 1', 'column 2', 'column name 3'], axis=1)

However, it is good practice to create a new data frame when dropping columns. If you run that cell again, you'll get errors.

df_dropped =  df.drop(['column 1', 'column 2', 'column name 3'], axis=1)

To answer your question about axis=1, it's the column headers.

column 1 column 2 column name 3
12 34 44
99 42 33
B S
  • 45
  • 7
  • Very clear, but I have a question, what does ‘inplace’ do in the second code block? – Yue Aug 31 '22 at 05:27
  • inplace=True modifies the original data. Anything else referencing that dataframe may have issues. You could think of it like ice cream, if you have a scoop of ice-cream on top of a cone, and then purposefully melt the ice cream, you can't get it back to the original state (scoop on the cone). – B S Sep 01 '22 at 13:24