Home > OS >  How to drop row which contains "no comment" string in column
How to drop row which contains "no comment" string in column

Time:09-24

How can i drop all rows in csv file that contains int the field "category" string ="no comment"?

csv file

CodePudding user response:

Try str.contains:

df = df.drop(df['category'].str.contains('no comment'))

CodePudding user response:

I assume you would want to use pandas for that. So, you have to read file as a dataframe, then select the rows by condition.

import pandas as pd

# read csv file
df = pd.read_csv('your_data.csv')

#select the rows by condition
result =  df[df['category'] != 'no comment']

CodePudding user response:

The below seems to work

import pandas as pd

data = [{'num':7,'category':'something'},{'num':79,'category':'no comment'}]
df = pd.DataFrame(data)
df = df.drop(df[df.category == 'no comment'].index)
print(df)

output

    category  num
0  something    7
  • Related