Home > Mobile >  Deleteing rows in a pandas dataframe if it contains a certain string
Deleteing rows in a pandas dataframe if it contains a certain string

Time:11-27

I have a list of columns in a dataframe that either contains a hashmark followed by a string or two hashmarks followed by a string. I wanted to eliminate the rows that contain only one hashmark.

df[df["column name"].str.contains("#") == False]

I've tried using the code above but it erased the entire column. I hoped that it would erase only the rows including only one hashmark. I do not know what to do.

CodePudding user response:

can you try this:

df['len']=df['column name'].str.count('#') #how many "#" expressions are in the column.

df=df[df["len"]>1]

#or one line

df=df[df['column name'].str.count('#')>1]

CodePudding user response:

if each of them have at least one '#' , and its either ## or #,

df[df["column name"].str.contains("##") == False]

above code will get you one #  ones.

df[df["column name"].str.contains("##") == True]

above code will eliminate #'s and get you ## ones.

  • Related