Python Forum
Drop rows if a set of columns has a value - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/Forum-Python-Coding)
+--- Forum: Data Science (https://python-forum.io/Forum-Data-Science)
+--- Thread: Drop rows if a set of columns has a value (/Thread-Drop-rows-if-a-set-of-columns-has-a-value)



Drop rows if a set of columns has a value - dervast - Sep-06-2019

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


RE: Drop rows if a set of columns has a value - sd_0912 - Sep-12-2019

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