请按月份画出2021年总体发标数量变化折线图。
时间: 2023-08-21 08:03:27 浏览: 39
假设数据已经被读取到了DataFrame对象`df`中,并且有一个名为`LoanDate`的日期列和一个名为`LoanAmount`的标的金额列。可以按照以下步骤绘制折线图:
1. 将`LoanDate`列转换为`datetime`类型,并将其设置为DataFrame的索引:
```
df['LoanDate'] = pd.to_datetime(df['LoanDate'])
df.set_index('LoanDate', inplace=True)
```
2. 使用`resample`方法按月进行重采样,并对`LoanAmount`列求和:
```
df_monthly = df['LoanAmount'].resample('M').sum()
```
3. 使用Matplotlib库绘制折线图:
```
import matplotlib.pyplot as plt
plt.plot(df_monthly.index, df_monthly.values)
plt.xlabel('Month')
plt.ylabel('Loan Amount')
plt.title('Monthly Loan Amount in 2021')
plt.show()
```
完整代码如下:
```
import pandas as pd
import matplotlib.pyplot as plt
# 读取数据到DataFrame对象df中
df['LoanDate'] = pd.to_datetime(df['LoanDate'])
df.set_index('LoanDate', inplace=True)
df_monthly = df['LoanAmount'].resample('M').sum()
plt.plot(df_monthly.index, df_monthly.values)
plt.xlabel('Month')
plt.ylabel('Loan Amount')
plt.title('Monthly Loan Amount in 2021')
plt.show()
```
相关推荐
![](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)