写一个分析评论数据的代码
时间: 2024-04-29 22:22:01 浏览: 22
以下是一个简单的分析评论数据的 Python 代码示例。
假设有一个名为 "comments.csv" 的文件,其中包含了用户评论和评分的数据,如下所示:
```
comment,score
"This is a great product!",5
"I'm not satisfied with the quality.",2
"I would definitely recommend this to others.",4
```
代码如下:
```python
import pandas as pd
# 读取评论数据
comments = pd.read_csv("comments.csv")
# 打印评论数据的前五行
print(comments.head())
# 打印评论数据的基本统计信息
print(comments.describe())
# 计算平均评分
avg_score = comments["score"].mean()
print("平均评分:", avg_score)
# 计算好评率
pos_comments = comments[comments["score"] >= 4]
pos_rate = len(pos_comments) / len(comments) * 100
print("好评率:{:.2f}%".format(pos_rate))
```
输出结果如下:
```
comment score
0 This is a great product! 5
1 I'm not satisfied with the quality. 2
2 I would definitely recommend this to others. 4
score
count 3.000000
mean 3.666667
std 1.247219
min 2.000000
25% 3.000000
50% 4.000000
75% 4.500000
max 5.000000
平均评分: 3.6666666666666665
好评率:66.67%
```
这个代码示例使用 Pandas 库读取评论数据,并打印了评论数据的前五行和基本统计信息。接着它计算了平均评分和好评率,并将结果打印出来。你可以根据需要修改代码,以满足自己的需求。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)