TypeError: может только объединять str (не "float") в str в DataFrame - PullRequest
1 голос
/ 07 апреля 2020

У меня есть кадр данных, который выглядит следующим образом:

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

Есть ли способ это исправить?

1 Ответ

2 голосов
/ 07 апреля 2020

Попробуйте:

neut_text+=str(df_copy.loc[i]["Text_en"])
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...