Home > Software design >  How to drop a dataset from a 2d DataFrame
How to drop a dataset from a 2d DataFrame

Time:11-28

I would like to delete a row/column from a 2d DataFrame. Let's assume the DataFrame looks like this:

animal   cat  dog  hedgehog
time
  0       1    1      0
  1       2    0      1

How to get rid of let's say the whole dog thingy to get something like that:

animal   cat hedgehog
time
  0       1    0
  1       2    1

I tried e.g. df.drop() with a lot of variations but haven't fully understood pandas yet.

CodePudding user response:

df.drop('dog',axis=1)

will drop it.You need to pass an axis. If you want this drop operation to effect current df , use inplace keyword.

df.drop('dog',axis=1,inplace=True)

If you want to drop more than one column, then pass a list.

df.drop(['dog','cat'],axis=1,inplace=True)

CodePudding user response:

You can remove the column, like this

df.drop(columns='dog', inplace=True)

and also you can remove many columns one time, like this

df.drop(columns=['dog', 'cat'], inplace=True)
  • Related