Reputation: 362
How to delete all the
title
,description
rows, except leaving the 1st one . I am usingPandas
as of now, so a solutions based on it will be helpful.
This is what I tried, but it didnt worked :
df = pd.read_csv(file)
df = df[df.name = "title"]
df.to_csv(file, index=False)
Upvotes: 0
Views: 135
Reputation: 56
Firstly find the indexes of title and description
index_list = df.index[df['title'] == 'title' or
df['description'] == 'description'].tolist()
Now drop all the rows that contain title and description.
df.drop(index=index_list, inplace=True)
At last write the data-frame in file
df.to_csv(file_name, index=False)
Upvotes: 3
Reputation: 492
Hopefully the first title,description
are assigned as headers of the dataframe.
In any case, you can use df.drop()
with some added zing to drop certain rows that meet your criteria:
Input:
title desc
0 abc2 something1
1 title desc
2 abc2 something2
3 title desc
4 abc3 something3
Using criteria where any row's title is literally called "title" and dropping them:
df = df.drop(df[df.title == "title"].index)
Output:
title desc
0 abc2 something1
2 abc2 something2
4 abc3 something3
Upvotes: 1
Reputation: 1603
You can keep the rows where the title is not 'title' :
df = df[df['title'] != "title"]
Upvotes: 2