Python Forum
Drop rows if a set of columns has a value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Drop rows if a set of columns has a value
#1
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
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Merging rows and adding columns based on matching index pythonnewbie78 3 748 Dec-24-2023, 11:51 AM
Last Post: Pedroski55
  read_csv error and rows/columns missing karlito 9 5,226 Nov-11-2019, 06:48 AM
Last Post: karlito
  drop rows that doesnt matched karlito 6 2,980 Oct-28-2019, 12:21 PM
Last Post: karlito
  display graph in columns and rows william888 1 1,824 Jul-02-2019, 10:19 AM
Last Post: dataman
  Drop rows from data with zero value Devilish 3 3,688 Dec-27-2018, 02:06 AM
Last Post: Devilish
  Dropping all rows of multiple columns after the max of one cell Thunberd 2 2,918 Jun-01-2018, 10:18 PM
Last Post: Thunberd
  Get rows with same value from dataframe of particular columns angelwings 1 2,708 Apr-11-2018, 02:40 AM
Last Post: scidam
  summing rows/columns more quickly jon0852 3 2,852 Feb-12-2018, 02:24 AM
Last Post: ka06059
  Stack dataframe columns into rows klllmmm 0 3,007 Sep-03-2017, 02:26 AM
Last Post: klllmmm

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020