0

I tried to drop a column and its corresponding row in a data frame in pandas using .drop(). But its dropping only the column and not the corresponding row that has the column value. For ex. I have unknown genre as a column and corresponding to it I have a movie as a row. When i drop the unknown column only the column is deleted but the movie still exist. I want to drop both the column and its corresponding row. Is there a single command to do the same.

Please find below the attachment of the dataframe

enter image description here

Ananya
  • 13
  • 3
  • Welcome to Stack Overflow. Please read how to ask good [questions](https://stackoverflow.com/help/how-to-ask). Make sure your question covers these 3 elements: 1. Problem Statement 2. Your Code (it should be [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) 3. Error Message (preferably full Traceback to help others review and provide feedback). Sometimes the same question may have already been asked. Make sure your question is not a [duplicate](https://stackoverflow.com/help/duplicates) – Joe Ferndz Mar 27 '21 at 22:17
  • To drop a row, you need to drop by index. You can use `df.loc` or `df.iloc` to drop the rows and then assign it to another dataframe (or same dataframe) – Joe Ferndz Mar 27 '21 at 22:21
  • Does this answer your question? [How to drop a list of rows from Pandas dataframe?](https://stackoverflow.com/questions/14661701/how-to-drop-a-list-of-rows-from-pandas-dataframe) – Joe Ferndz Mar 27 '21 at 22:21
  • Please provide at least a sample of your DataFrame, error or unwanted result and expected result. – Max Pierini Mar 27 '21 at 22:23
  • Alternate is to convert your search to a boolean result set and then use the boolean to filter the dataframe – Joe Ferndz Mar 27 '21 at 22:28

1 Answers1

0

Try the other way around. First retain only the rows of your dataframe for which value in "unknown" column is not equal to 1 and then get rid of the column:

new_df = df.loc[~(df["unkwown"] == 1), :].drop(columns="unknown")
Laurent
  • 12,287
  • 7
  • 21
  • 37