簡體   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