1

I have this text file:

LEU,LEID,PPP,YYY,LEO '1','2','3','4','5' '2','1','2','3','4' '2','AA','','','' 

I want to delete the rows where for LEID='1'

import pandas as pd import os originalFile=os.path.abspath("D:\\python\\test\\OriginalFile.csv") df = pd.read_csv(originalFile) df = df[(df['LEID'] !='1')] df.to_csv('D:\\python\\test\\CorrectedFile.csv') print (df) 

Why the row with LEID='1' is not delted?

2 Answers 2

1

As you have it, the quote characters are still included in your dataframe. It probably looks like:

>>> df LEU LEID PPP YYY LEO 0 '1' '2' '3' '4' '5' 1 '2' '1' '2' '3' '4' 2 '2' 'AA' '' '' '' 

So you would actually need to include those quotes in your filter. You can do this like:

df[df['LEID'] != "'1'"] # or: df[df['LEID'] !='\'1\''] LEU LEID PPP YYY LEO 0 '1' '2' '3' '4' '5' 2 '2' 'AA' '' '' '' 

An alternative would be to remove your single quotes when you read the csv:

df = pd.read_csv(originalFile, sep=',', quotechar="'") 

And you will get this dataframe:

>>> df LEU LEID PPP YYY LEO 0 1 2 3.0 4.0 5.0 1 2 1 2.0 3.0 4.0 2 2 AA NaN NaN NaN 

Which you can filter like you were trying:

df[df['LEID' ]!= '1'] LEU LEID PPP YYY LEO 0 1 2 3.0 4.0 5.0 2 2 AA NaN NaN NaN 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for the help. It is strange as I tried both with removing the quotes before import and for some reason I still did not managed.
0

df[(df['LEID'] !='1')] selects on columns. You need df.loc[df['LEID'] !='1']

1 Comment

Both the methods you provided would work equivalently for most applications in a normal case, but I don't think that's where OP's problem is coming from. I believe it has to do with the quote characters

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.