简体   繁体   中英

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

I have a dataframe which look like:

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         |   
+---------------------------------------------------+-------------+------------+------------+ 

I want to generate/assigned positive text, negative text, neutral text for further processing using this code:

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]

But it returns

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

Is there a way to fix it?

Try:

neut_text+=str(df_copy.loc[i]["Text_en"])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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