Bottom Page

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
Quote
#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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  display graph in columns and rows william888 1 259 Jul-02-2019, 10:19 AM
Last Post: dataman
  Drop rows from data with zero value Devilish 3 587 Dec-27-2018, 02:06 AM
Last Post: Devilish
  Dropping all rows of multiple columns after the max of one cell Thunberd 2 629 Jun-01-2018, 10:18 PM
Last Post: Thunberd
  Get rows with same value from dataframe of particular columns angelwings 1 648 Apr-11-2018, 02:40 AM
Last Post: scidam
  summing rows/columns more quickly jon0852 3 770 Feb-12-2018, 02:24 AM
Last Post: ka06059
  Stack dataframe columns into rows klllmmm 0 1,149 Sep-03-2017, 02:26 AM
Last Post: klllmmm

Forum Jump:


Users browsing this thread: 1 Guest(s)