Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

IOB format merge

I have a dataframe in IOB format as below:-

Name Label
Alan B-PERSON
Smith I-PERSON
is O
Alice’s B-PERSON
uncle O
from O
New B-LOCATION
York I-LOCATION
city I-LOCATION

I would like to convert into a new dataframe as below:-

Name Label
Alan Smith PERSON
Alice’s PERSON
New York city LOCATION

Any help is much appreciated!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can create groups by compare values O, remove IO- values in Label column and with helper groups created by cumulative sum aggregate join:

m = df['Label'].eq('O')

df = (df[~m].assign(Label=lambda x: x['Label'].str.replace('^[IB]-', ''))
            .groupby([m.cumsum(), 'Label'])['Name']
            .agg(' '.join)
            .droplevel(0)
            .reset_index()
            .reindex(df.columns, axis=1))
print (df)
            Name     Label
0     Alan Smith    PERSON
1        Alice's    PERSON
2  New York city  LOCATION
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading