Home > Enterprise >  How do I remove a rows from a dataframe based on column full match and partial match of another colu
How do I remove a rows from a dataframe based on column full match and partial match of another colu

Time:10-06

I have a dataframe that contains several columns and I want to drop values based on two criterias: First: Column name should full match my criteria Second: Column description should partially match another criteria

If both are true, I should remove the row.

Example: I want to drop every row that contains Blue or Green on the first column and contains "abc" on the second one.

Name Description
Blue abcdefg
Red hrqt
Blue abcde
Green abcd
Black jfg

Using python v.3.0 or more to solve it.

CodePudding user response:

df.drop(((df["Name"]=="Blue") | (df["Name"]=="Green")) & (df["Description"].str.contains("abc")))

CodePudding user response:

You can set two conditions before using df.loc.

cond1 = df.Name.isin(['Blue','Green'])
cond2 = df.Description.str.contains('abc')

dfd.loc[~(cond1 & cond2)]

    Name Description
1    Red        hrqt
4  Black         jfg
  • Related