更改后出现下面的报错怎么修改:Traceback (most recent call last): File "D:\kelly\PycharmProjects\pythonProject6\网络预测.py", line 73, in <module> df['阅读量得分'] = df['阅读量'].apply(calc_read_score) File "D:\python3.10\lib\site-packages\pandas\core\series.py", line 4771, in apply return SeriesApply(self, func, convert_dtype, args, kwargs).apply() File "D:\python3.10\lib\site-packages\pandas\core\apply.py", line 1123, in apply return self.apply_standard() File "D:\python3.10\lib\site-packages\pandas\core\apply.py", line 1174, in apply_standard mapped = lib.map_infer( File "pandas\_libs\lib.pyx", line 2924, in pandas._libs.lib.map_infer File "D:\kelly\PycharmProjects\pythonProject6\网络预测.py", line 25, in calc_read_score read_num = int(read_num) ValueError: invalid literal for int() with base 10: '457.0'
时间: 2023-07-21 09:15:38 浏览: 136
lightgbm报错:OSError [WinError 126] 找不到指定的模块-三种方法
根据报错信息,看起来是在 `calc_read_score` 函数中出现了类型错误,因为 `read_num` 是字符串类型,但其中包含一个小数点。`int()` 函数只能将字符串类型的整数转换成整型,无法处理带小数点的字符串。
你需要在 `calc_read_score` 函数内部,将 `read_num` 转换成浮点型,例如可以使用 `float()` 函数进行转换,如下所示:
```python
def calc_read_score(read_num):
read_num = float(read_num) # 将 read_num 转换成浮点型
if read_num <= 1000:
return 1
elif read_num <= 5000:
return 2
elif read_num <= 10000:
return 3
else:
return 4
```
修改后再次运行代码,应该就不会再出现这个错误了。
阅读全文