如何在 pandas 帧中显示 IBM 音调分析器的输出

How to display output of IBM tone analyzer in pandas frame

我的数据集具有以下特征:“描述”、“word_count”、“char_count”、“停用词”。特征“描述”的数据类型为包含一些文本的字符串。我正在做 IBM tone_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)

我需要将上述输出保存在 pandas 数据框中。

Series.apply中使用lambda函数:

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