0

My pandas dataframe is this one and as you can see, the text 2 is composed of 2 forbidden words. As a result, I would like to duplicate this row, the first one with only "[stupid]" and the latter with "[sex]". Consequently, the dataframe will now have 5 rows (two with "text3" instead of 4).

The order of the length are not very important.

temp = pd.DataFrame({
    "Text" : ["text1","text2","text3","text4"],
    "ForbiddenWord" : ["[]", "[fat]",["stupid","sex"],[""]],
    "Total" : [0,1,2,0]
    })

I tried to figure our this problem with a loop with playing with index is quite hard and dangerous. I'm pretty sure that a pandas function is able to see the length of a value and cut each element it into several rows.

  • You should only use strings, or only lists, not combine both. If you have strings, first convert to lists with `ast.literal_eval` – mozway Apr 28 '23 at 08:45

0 Answers0