python - TypeError:只能将str(不是“float”)连接到DataFrame中的str
问题描述
我有一个看起来像的数据框:
df:
+---------------------------------------------------+-------------+------------+------------+
|Text_en | pos_score | neg_score | sent_score|
+---------------------------------------------------+-------------+------------+------------+
| inspir afternoon bang stori ahmad sahroni v AS...| 0.000 | 0.0 | 0 |
| | 0.000 | 0.0 | 0 |
| some drastic measur taken manag bodi temperatu. | 1.625 | 0.5 | 1 |
| ahmad sahroni tu | 0.000 | 0.0 | 0 |
| busi success mudah legisl mandat who make inte...| 1.125 | 0.0 | 1 |
+---------------------------------------------------+-------------+------------+------------+
我想使用以下代码生成/分配正面文本、负面文本、中性文本以进行进一步处理:
pos_text=""
neg_text=""
neut_text=""
for i in range(len(df_copy.index)):
if(df_copy.loc[i]["sent_score"]==1):
pos_text+=df_copy.loc[i]["Text_en"]
elif(df_copy.loc[i]["sent_score"]==-1):
neg_text+=df_copy.loc[i]["Text_en"]
else:
neut_text+=df_copy.loc[i]["Text_en"]
list_text = [pos_text,neg_text,neut_text]
但它返回
---------------------------------------------------------------------------
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-44-67c89339edeb> in <module>
5 neg_text+=df_copy.loc[i]["Text_en"]
6 else:
----> 7 neut_text+=df_copy.loc[i]["Text_en"]
8
9 list_text = [pos_text,neg_text,neut_text]
TypeError: can only concatenate str (not "float") to str
有没有办法解决它?
解决方案
尝试:
neut_text+=str(df_copy.loc[i]["Text_en"])
推荐阅读
- javascript - 十一怎么有相对路径?
- abap - 在 SAP CRM Web UI 中将 0 替换为空白
- java - 可滚动编辑文本在滚动视图内
- java - SetLenient(false) 与年份
- python - 为什么 ROS 发布者不发布第一条消息?
- python - ImportError:无法在简单的烧瓶蓝图应用程序上运行 pytest
- python-3.x - 如何处理python中发生的分段错误
- c# - 找不到方法:'System.Threading.Tasks.Task`1
Microsoft.Rest.Azure.Authentication.UserTokenProvider - oracle - 从核心 1.0 升级到 2.2
- html - node_modules/ngx-bootstrap/chronos/utils/type-checks.d.ts(8,62) 中的错误:错误 TS2304:找不到名称“提取”