繁体   English   中英

TypeError:只能将 str(不是“float”)连接到 DataFrame 中的 str

[英]TypeError: can only concatenate str (not “float”) to str in a DataFrame

我有一个 dataframe 看起来像:

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"])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM