Skip to content
Advertisement

I need to add specific rows in pandas DataFrame, at specific position

I’m currently working on a project and I need to add specific rows whenever the tagged sentence ends. Whenever the ‘N’ column equals 1 it means that a new sentence started. I want to add two rows for each sentence: a row with ‘Pos’= START at the beginning of the sentence, and a row with ‘Pos’=End at the end of each row. This is what the DataFrame look like:

JavaScript

In this case I need a [Nan, Nan, START] tag at indexes 0 and 15. and a [Nan,Nan, END] tag at index 14. I need to make it for all my df. How could I do this?

Advertisement

Answer

Analyzing your dataframe, I just assume you want to insert START before value 1 in column N and insert END after the max continuous value in column N. If so, you could do following

First create two dummy dataframe start_df and end_df

JavaScript

Then split the dataframe with continuous value in column N

JavaScript

Moreover, insert dummy dataframe before and after each group

JavaScript

At last, create dataframe by concating dataframe in list

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement