还是报错line 8, in <module> data["semiscore"] = data['评论内容'].apply(lambda x: SnowNLP(x).sentiments)
时间: 2024-03-23 13:38:37 浏览: 12
这个错误提示可能是由于某些评论内容缺失或格式不正确,导致 `SnowNLP` 函数无法计算情感分数而引起的。你可以尝试添加一些代码来调试:
```
def calculate_sentiment(x):
try:
return SnowNLP(x).sentiments
except Exception as e:
print(f"Error occurred while calculating sentiment for comment '{x}': {e}")
return None
data["semiscore"] = data['评论内容'].apply(calculate_sentiment)
```
这个修改会将 `SnowNLP` 函数的调用包装在一个新的函数 `calculate_sentiment` 中,并使用 `try-except` 语句来捕获异常。如果在计算情感分数时发生异常,会打印一个错误消息,并将情感分数设为 `None`。这样做的目的是帮助你找出哪些评论导致了错误,以便你进一步调查和修复问题。
请尝试以上修改,并查看输出结果,看看是否有任何错误消息或 `None` 值。如果有,请检查这些评论的内容,看看它们是否有任何格式问题、特殊字符或其他不正常的东西。如果需要,你可以手动从 CSV 文件中删除这些评论,然后重新运行代码。
相关问题
Traceback (most recent call last): File "C:/Users/86186/Desktop/ceshi.py", line 36, in <module> year_month = data['time'][0].apply(lambda x: x.strftime('%Y-%m')) AttributeError: 'Timestamp' object has no attribute 'apply'
这个错误是因为 `data['time'][0]` 是一个 `Timestamp` 类型的对象,而 `apply()` 函数是 Pandas 中 Series 对象的方法,不能直接应用于 `Timestamp` 类型的对象上。
你可以尝试将 `apply()` 函数替换为 `strftime()` 函数,将时间戳格式化为指定格式的字符串,如下所示:
```python
year_month = data['time'][0].strftime('%Y-%m')
```
这里使用 `strftime()` 函数将时间戳格式化为指定格式的字符串,并将其存储在 `year_month` 变量中。
希望这个解决方法能够帮助你解决问题。
Traceback (most recent call last): File "C:\Users\MAC\Desktop\py\筛选股票\121.py", line 9, in <module> today = datetime.date.today().strftime('%Y-%m-%d') AttributeError: 'method_descriptor' object has no attribute 'today'
这个错误通常是因为 `datetime.today()` 和 `datetime.date.today()` 这两个方法名重复导致的。可以尝试使用完整的模块名来调用 `today()` 方法,即将 `datetime.date.today()` 替换为 `datetime.datetime.today().date()`。修改后的代码如下:
```python
import tushare as ts
import pandas as pd
import datetime
# 设置tushare token
ts.set_token('your_token_here')
# 获取今日日期
today = datetime.datetime.today().date().strftime('%Y-%m-%d')
# 获取今日及今日前四个交易日的日期
days = [datetime.datetime.today().date() - datetime.timedelta(days=i) for i in range(5)]
dates = [d.strftime('%Y-%m-%d') for d in days]
# 获取沪市、深市、创业板、科创板股票数据
data = pd.DataFrame()
for date in dates:
hs_data = ts.get_hist_data('sh', date=date)
if hs_data is not None:
hs_data['type'] = 'sh'
data = data.append(hs_data)
sz_data = ts.get_hist_data('sz', date=date)
if sz_data is not None:
sz_data['type'] = 'sz'
data = data.append(sz_data)
cyb_data = ts.get_hist_data('cyb', date=date)
if cyb_data is not None:
cyb_data['type'] = 'cyb'
data = data.append(cyb_data)
kcb_data = ts.get_hist_data('kcb', date=date)
if kcb_data is not None:
kcb_data['type'] = 'kcb'
data = data.append(kcb_data)
# 保存数据
data.to_csv(f'{today}_data.csv', index=False)
# 读取数据
data = pd.read_csv(f'{today}_data.csv')
# 筛选出五日内每日成交额最多的100支股票
top100 = data.groupby(['code'])['amount'].sum().sort_values(ascending=False)[:100].index.tolist()
data = data[data['code'].isin(top100)]
# 计算股票今日及前四个交易日收盘价的均价
data['close_mean'] = data.groupby(['code'])['close'].transform(lambda x: x.mean())
# 选出今日收盘价大于五日均价且今日成交量为三日内最少的股票
data = data[data['close'] > data['close_mean']]
data = data.groupby(['code']).apply(lambda x: x.nsmallest(3, 'volume'))
data = data.groupby(['code']).apply(lambda x: x.nlargest(1, 'date'))
data = data.reset_index(drop=True)
# 保存结果到文件
data.to_csv(f'{today}_result.csv', index=False)
```
希望能解决你的问题。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)