python用pandas读取csv文件,在评论时间这一列中,将同一年的评论选择出来,再将其每个月的评分在一张图上画散点
时间: 2024-02-03 11:11:49 浏览: 25
可以按照以下步骤实现:
1. 导入 pandas 和 matplotlib 库:
```python
import pandas as pd
import matplotlib.pyplot as plt
```
2. 使用 pandas 读取 csv 文件:
```python
df = pd.read_csv('your_file.csv')
```
3. 将评论时间列转换为 datetime 类型,并将其设置为索引:
```python
df['评论时间'] = pd.to_datetime(df['评论时间'])
df.set_index('评论时间', inplace=True)
```
4. 选择同一年的评论:
```python
df_year = df.loc['2021']
```
5. 按月份统计评分均值:
```python
df_monthly = df_year.groupby(df_year.index.month)['评分'].mean()
```
6. 将评分均值画成散点图:
```python
plt.scatter(df_monthly.index, df_monthly.values)
plt.xlabel('Month')
plt.ylabel('Average Rating')
plt.show()
```
完整代码如下:
```python
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('your_file.csv')
df['评论时间'] = pd.to_datetime(df['评论时间'])
df.set_index('评论时间', inplace=True)
df_year = df.loc['2021']
df_monthly = df_year.groupby(df_year.index.month)['评分'].mean()
plt.scatter(df_monthly.index, df_monthly.values)
plt.xlabel('Month')
plt.ylabel('Average Rating')
plt.show()
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)