我的数据集有以下特性:“描述”、"word_count“、"char_count”、“秒表”。特性"description“的数据类型为string,其中包含一些文本。我正在对这个特性执行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)我需要将上述输出保存在熊猫的数据框架中。
发布于 2021-03-11 08:35:38
在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))https://stackoverflow.com/questions/66578897
复制相似问题