0 votes
1 view
in Data Science by (17.1k points)

I have a dataframe which is something like this

Victim Sex       Female    Male  Unknown

Perpetrator Sex                         

Female            10850   37618       24

Male              99354  299781       92

Unknown           33068  156545      148

I'm planning to drop both the row indexed as 'Unknown' and the column named 'Unknown'. I know how to drop a row and a column but I was wondering whether you could drop a row and a column at the same time in pandas? If yes, how could it be done?

1 Answer

0 votes
by (29.5k points)

The below code will give you the required output, not at the same time, but it will not return any intermediate object.

df.drop("Unknown", axis=1).drop("Unknown", axis=0)

Example:

df = pd.DataFrame([[1,2],[3,4]], columns=['A', 'B'], index=['C','D'])

print(df)

     A    B

C  5     8

D  9     6

then call

df.drop('B', axis=1).drop('C', axis=0)

returns

     A

D  9

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...