我是用拉格朗日插值法处理表格的异常值,但程序到第20行时报错。请问出错点在哪?如何解决?
部分表格和代码如下:
| 日期| 单品编码 | 批发价格(元/千克) |
| 2022-07-01 | 102900005115762 | 3.88 |
| 2022-07-02| 102900005115763 | 6.72 |
| 2022-07-03 | 102900005115764 | 3.19 |
import os
import pandas as pd
import numpy as np
from scipy.interpolate import lagrange
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
# 数据的读取
data = pd.read_excel("C:/Users/YHS/Desktop/附件3.xlsx")
neg_list = data['批发价格(元/千克)']
# 数据的行数
R = data.shape[0]
# 异常数据的个数
for item in neg_list:
iqr = data[item].quantile(0.75) - data[item].quantile(0.25)
q_abnormal_L = data[item] < data[item].quantile(0.25) - 1.5 * iqr
q_abnormal_U = data[item] > data[item].quantile(0.75) + 1.5 * iqr
print(item + '中有' + str(q_abnormal_L.sum() + q_abnormal_U.sum()) + '个异常值')
```python
发生异常: KeyError
3.88
KeyError: 3.88
The above exception was the direct cause of the following exception:
File "C:\Users\YHS\Desktop\拉格朗日插值法.py", line 20, in <module>
iqr = data[item].quantile(0.75) - data[item].quantile(0.25)
~~~~^^^^^^
KeyError: 3.88