gpt4 book ai didi

python - 如何通过访问数据框而不是python中的csv进行情感分析?

转载 作者:行者123 更新时间:2023-12-02 04:19:51 25 4
gpt4 key购买 nike

我试图了解如何将csv逻辑应用于脚本中已经存在的数据帧输出。
csv的情绪分析

import pandas as pd
import csv
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer

with open('After.csv', "r", errors='ignore') as f:
reader = csv.reader(f)
your_list = list(reader)

analyser = SentimentIntensityAnalyzer()

def print_sentiment_scores(alist):
for aSentence in alist:
aSnt = analyser.polarity_scores(aSentence[0])
print(str(aSnt))

df_before = print_sentiment_scores(your_list)

print_sentiment_scores(your_list)


def print_sentiment_scores(alist):
polarity_scores = []
for aSentence in alist:
aSnt = analyser.polarity_scores(aSentence[0])
print(str(aSnt))
polarity_scores += [aSnt]

return polarity_scores

output_df = pd.DataFrame(print_sentiment_scores(your_list))
output_df.to_csv('some_name.csv')
我有一个包含4列的数据框,其中有一个成绩单
test3
Out[52]:
confidence ... speaker
0 0.86 ... 0
1 0.91 ... 0
2 0.94 ... 0
3 0.86 ... 0
4 0.99 ... 0
[353行x 5列]
test3['transcript']
Out[51]:
0 thank you for calling my name is Britney and h...
1 thank you %HESITATION and then %HESITATION you...
2 what is last week this
3 so did you want to cancel it effective the ten...
4 %HESITATION

Name: transcript, Length: 353, dtype: object
如何将我上面的情感分析脚本应用于test3数据框中的此特定列(脚本)?

最佳答案

使用to_list()转换列并应用您的函数,在本示例中,我使用函数'f',因为您没有提供示例数据。

import pandas as pd

df = pd.DataFrame({'A': ['aba', 'flower'], 'B': ['we search stackoverflow', 'good question']})
print('Print dataframe content:', df)


def print_sentiment_scores(x):
return x +' '+ x

print()
for word in (df['B'].to_list()):
print("Print output of the function applied to series:",print_sentiment_scores(word))

出:
Print dataframe content:         A                        B
0 aba we search stackoverflow
1 flower good question

Print output of the function applied to series: we search stackoverflow we search stackoverflow
Print output of the function applied to series: good question good question

here is simple interactive example

关于python - 如何通过访问数据框而不是python中的csv进行情感分析?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61148244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com