Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
251 views
in Technique[技术] by (71.8m points)

pandas - Creating a new column with a iterating sentence count whenever two simultaneous row values are null (indicating a new sentence is found)

I have a dataframe with words and entities and would like to create a third column which keeps a sentence count for every new sentence found as shown in the link example of desired output.

The condition based on which I would recognize the start of a new sentence is when both the word and entity columns have null values like at index 4.

0     word        entity
1      It           O
2      was          O
3      fun          O
4      NaN          NaN
5      from         O
6      vodka        B-product    

So far I have managed to fill the null values with a new_sent string and have figured out how to make a new column where I can enter a value whenever a new sentence is found using.

df.fillna("new_sentence", inplace=True)
df['Sentence #'] = np.where(df['word']=='new_sentence', 'S', False)

In the above code instead of S I would like to fill Sentence: {count} as in the example. What would be easiest/quickest way to do this? Also, is there a better way to keep a count of sentences in a separate column like in the example instead of the method I am trying?

So far I am able to get an output like this

0     word        entity        Sentence #
1      It           O             False
2      was          O             False
3      fun          O             False
4      new_sentence new_sentence   S
5      from         O             False
6      vodka        B-product     False
question from:https://stackoverflow.com/questions/66055833/creating-a-new-column-with-a-iterating-sentence-count-whenever-two-simultaneous

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...