if __name__ == '__main__': # 读取数据 df = pd.read_excel('data_guba_cjwl (2).xlsx',names=['阅读量','发帖时间', '评论数', '帖子标题', '帖子链接']).astype(str) # 计算信息量指标得分 df['时间得分'] = df['发帖时间'].apply(calc_time_score) df['阅读量得分'] = df['阅读量'].apply(calc_read_score) df['评论数得分'] = df['评论数'].apply(calc_comment_score) df['标题得分'] = df['帖子标题'].apply(calc_title_score) # 计算信息量指标总得分 df['信息量指标得分'] = df['时间得分'] * df['阅读量得分'] * df['评论数得分'] * df['标题得分'] # 输出结果 print(df)这段代码出现下面报错要怎么修改? File "D:\kelly\PycharmProjects\pythonProject6\网络预测.py", line 74, 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 if read_num <= 1000: TypeError: '<=' not supported between instances of 'str' and 'int'
时间: 2023-12-19 17:07:19 浏览: 62
Guba_Xueqiu_Crawler:搜寻来自guba.eastmoney.com和xueqiu.com的帖子
根据报错信息,看起来是在 `calc_read_score` 函数中出现了类型错误,因为 `read_num` 是字符串类型,而 `1000` 是整型,无法进行大小比较。
你需要在 `calc_read_score` 函数内部,将 `read_num` 转换成整型,例如可以使用 `int()` 函数进行转换,如下所示:
```python
def calc_read_score(read_num):
read_num = int(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
```
修改后再次运行代码,应该就不会再出现这个错误了。
阅读全文