Python Forum

Full Version: Drop rows if a set of columns has a value
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
I am looking for a way in pandas to do the following:

First lets assume a data frame with the following columns

small_df=df[['1','2','3','4','5','dist','unique']]
then I would like to

drop any row if any of the columns 1,2,3,4,5 contains 100 or 101?

How I can combine all those conditions together?

Thanks a lot
Regards
Alex
Instead of dropping the rows, you need to filter the dataframe instead, something like this:

import pandas as pd
small_df=pd.DataFrame(columns=['1','2','3','4','5','dist','unique'])
small_df = small_df.append([{
  "1": 100,
    "2": "Test",
    "3": "Test"
},
{
    "1": "Test",
    "2": 100,
    "3": "Test"
},
{
    "1": "Test",
    "2": "Test",
    "3": "Test"
}], ignore_index=True)

small_df

small_df = small_df[(small_df['1'] == 100) | (small_df['2'] == 100) | (small_df['3'] == 100) | (small_df['4'] == 100)]
small_df