如何通过迭代定义的函数将值填充到新列中?

How to fill values into a new column by iterating a defined function?

自学真的很费功夫,在这个问题上浪费了很多时间后,我决定来这里寻求帮助。所以,我正在为我的项目使用 Google api,如下所示:

def sample_analyze_sentiment(text_content):
    """
    Analyzing Sentiment in a String

    Args:
      text_content The text content to analyze
    """

    client = language_v1.LanguageServiceClient()

    # text_content = 'I am so happy and joyful.'

    # Available types: PLAIN_TEXT, HTML
    type_ = language_v1.Document.Type.PLAIN_TEXT

    # Optional. If not specified, the language is automatically detected.
    # For list of supported languages:
    # https://cloud.google.com/natural-language/docs/languages
    language = "en"
    document = {"content": text_content, "type_": type_, "language": language}

    # Available values: NONE, UTF8, UTF16, UTF32
    encoding_type = language_v1.EncodingType.UTF8

    response = client.analyze_sentiment(request = {'document': document, 'encoding_type': encoding_type})
    # Get overall sentiment of the input document
    print(format(response.document_sentiment.score))

而我想做的是通过这个函数迭代文本数据以获得每一行的情感分数。

df = pd.read_csv('Final.csv')
for items in df['Text']:
    sample_analyze_sentiment(items)
    

然后将其映射到数据框的新列(这是我不知道如何做的地方)。我的函数 return string of floats 但它绝对是非类型的,所以我想我不能这样做。但是,我仍然怀疑它似乎可行。

请帮忙

您可以尝试 apply() 函数到数据框中的 'Text' 列。为此,您必须 return 您的函数结果(即情绪):

def sample_analyze_sentiment(text_content):
    """
    Analyzing Sentiment in a String

    Args:
      text_content The text content to analyze
    """

    client = language_v1.LanguageServiceClient()

    # text_content = 'I am so happy and joyful.'

    # Available types: PLAIN_TEXT, HTML
    type_ = language_v1.Document.Type.PLAIN_TEXT

    # Optional. If not specified, the language is automatically detected.
    # For list of supported languages:
    # https://cloud.google.com/natural-language/docs/languages
    language = "en"
    document = {"content": text_content, "type_": type_, "language": language}

    # Available values: NONE, UTF8, UTF16, UTF32
    encoding_type = language_v1.EncodingType.UTF8

    response = client.analyze_sentiment(request = {'document': document, 'encoding_type': encoding_type})
    # Get overall sentiment of the input document
    return format(response.document_sentiment.score)

然后应该可以使用以下行在您的数据框中创建一个新列 'sentiment'

df['sentiment'] = df['Text'].apply(sample_analyze_sentiment)