Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k 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 (41.4k 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

learn Pandas with the help of this Pandas Tutorial.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...