Python Forum
Drop a row if it contains a certain value in pandas
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Drop a row if it contains a certain value in pandas
#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'

I wrote this code:
import pandas as pd

#import numpy
import os

originalFile=os.path.abspath("D:\\python\\test\\OriginalFile.csv")
df = pd.read_csv(originalFile)
#df.drop(df.LEID == '1')


df=df[df.LEID != '1']

df.to_csv('D:\\python\\test\\CorrectedFile.csv')
print (df)
Why the row with LEID='1' is not delted?
Reply
#2
I would check to see if pd.read_csv is doing a type conversion such that LEID is an int rather than a string. If you want to keep it as a string, you can specify that with the dtype parameter. Otherwise you need to test for an int: df = df[df.LEID != 1].
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thanks @ichabod801. Apparently the problem is with the quotes.
Reply
#4
import pandas as pd

#import numpy
import os
import csv

originalFile=os.path.abspath("C:\\Users\\monis\\Desktop\\Document1.csv")
df = pd.read_csv(originalFile)
df = df[df.LEID != '1']

df.to_csv('C:\\Users\\monis\\Desktop\\correctedfile.csv')
print (df)

Output
LEU LEID PPP YYY LEO
0 1 2 3.0 4.0 5.0
2 2 'AA' NaN NaN NaN
Reply
#5
import pandas as pd
import os
import csv

originalFile=os.path.abspath("C:\\Users\\mo\\Desktop\\Document1.csv")
df = pd.read_csv(originalFile)
df = df[df.LEID != '1']

df.to_csv('C:\\Users\\mo\\Desktop\\correctedfile.csv')
print (df)
Output:
LEU LEID PPP YYY LEO 0 1 2 3.0 4.0 5.0 2 2 'AA' NaN NaN NaN
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to drop column in pandas SriMekala 3 2,924 Aug-26-2019, 06:36 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020