2

我的数据集具有以下特征:“description”、“word_count”、“char_count”、“stopwords”。功能“描述”的数据类型为包含一些文本的字符串。我正在对这个特性进行 IBMtone_analysis,它给了我正确的输出,看起来像这样:

[{'document_tone': {'tones': [{'score': 0.677676,
     'tone_id': 'analytical',
     'tone_name': 'Analytical'}]}},
 {'document_tone': {'tones': [{'score': 0.620279,
     'tone_id': 'analytical',
     'tone_name': 'Analytical'}]}},    

上面的代码如下:

result =[]
for i in new_df['description']:
   tone_analysis = ta.tone(
       {'text': i},
     #  'application/json'
   ).get_result()
   result.append(tone_analysis)

我需要将上述输出保留在熊猫数据框中。

4

1 回答 1

0

使用 lambda 函数Series.apply

new_df['new'] = new_df['description'].apply(lambda i: ta.tone({'text': i}).get_result())

编辑:

def f(i):
    x = ta.tone({'text': i}).get_result()['document_tone']['tones']
    return pd.Series(x[0])


new_df = new_df.join(new_df['description'].apply(f).drop('tone_id', axis=1))
print (new_df)

如果需要还删除description列:

new_df = new_df.join(new_df.pop('description').apply(f).drop('tone_id', axis=1))
于 2021-03-11T08:35:38.527 回答