Home > Net >  How to delete a first and second line in each cell of column in csv
How to delete a first and second line in each cell of column in csv

Time:10-06

I didnt find solution for it, so maybe someone can help me

I have an csv file and it has "splitted_text" column, how can I delete the first and second line of text in each row? enter image description here

CodePudding user response:

Based on the screenshot, I'm assuming that by:

delete the first and second line of text

you mean "delete the first and second element of each list contained in the splitter_text column"

In which case, you can simply apply a function along the column:

df["clean_splitted_text"] = df["splitted_text"].apply(lambda x: x[2:])

to save the cleaned output in a new column, or

df["splitted_text"] = df["splitted_text"].apply(lambda x: x[2:])

if you want to overwrite the content of that column (!)

Here, lambda x: x[2:] is a function that removes the first two elements of a list. If that list has 2 or fewer elements, then it returns the empty list: [].

  • Related