有没有办法将每个结果添加到数据框的一行?

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

我正在研究一种注释文本的方法,目前正在构建一个函数来将每个文本及其位置添加到数据框的一行中。

文本:位置:

苹果 PROPN 成为辅助 看动词

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  billion')

尝试收集数据,然后创建数据框。一般来说,这 运行 比将行附加到现有数据框更有效:

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

然后调用它:

df = annotate('Apple is looking at buying U.K. startup for  billion')