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

Is there a way to add each result to a row of the dataframe?

I’m working on a method to annotate a text and currently building a function to add each text and its pos to a row on the dataframe.

Text: pos :

apple PROPN
be AUX
look VERB

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

import spacy
import pandas as pd

df = pd.DataFrame(columns = ['Text', 'pos'])

def annotate(text):
    nlp = spacy.load("en_core_web_sm")
    doc = nlp(text)

    for token in doc:
        print(token.text, token.pos_) 
        df = df.append({'Text' : 'token.text', 'pos' : 'token.pos_'},  ignore_index = True)

annotate('Apple is looking at buying U.K. startup for $1 billion')

>Solution :

Try collecting the data, THEN creating the dataframe. In general that will run more efficiently than appending rows to an existing dataframe:

def annotate(text):
    nlp = spacy.load("en_core_web_sm")
    doc = nlp(text)

    rows = []
    for token in doc:
        print(token.text, token.pos_)
        rows.append([token.text, token.pos])
    df = pd.DataFrame(rows, columns=['Text', 'pos'])
    return df

then call it using:

df = annotate('Apple is looking at buying U.K. startup for $1 billion')
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