Barak
Barak

Reputation: 61

pandas drop rows based on cell content and no headers

I'm reading a csv file with pandas that has no headers.

df = pd.read_csv('file.csv', header=0) 

csv file containing 1 row with several users:

admin
user
system
sysadmin
adm
administrator

I need to read the file to a df or a list except for example: sysadmin and save the result to the csv file

admin
user
system
adm
administrator

Upvotes: 2

Views: 289

Answers (2)

jezrael
jezrael

Reputation: 863741

Select first columns, filter by boolean indexing and write to file:

df = pd.read_csv('file.csv', header=0) 

df[df.iloc[:, 0].ne('sysadmin')].to_csv(file, index=False)

#if there is csv header converted to column name
#df[df['colname'].ne('sysadmin')].to_csv(file, index=False)

If no header in csv need parameters like:

df = pd.read_csv('file.csv', header=None) 
df[df.iloc[:, 0].ne('sysadmin')].to_csv(file, index=False, header=False)

Upvotes: 3

Govind129
Govind129

Reputation: 11

you can give it a try:---

df = pd.read_csv('file.csv') 
df=df.transpose()

df.columns=['admin','user','system','sysadmin','adm','administrator']
df.head()

I think this will work it out. if not then try it:

 df = pd.read_csv('file.csv') 


df.columns=['admin','user','system','sysadmin','adm','administrator']
df.head()

Upvotes: 0

Related Questions