2017-04-19 3 views
1

텍스트가있는 데이터 프레임이 있습니다. textblob을 적용하고 각 행에 대한 감정가를 계산하고 싶습니다.데이터 프레임의 각 행에 textblob 적용

text    sentiment 

이 중대하다
큰 영화 좋은 이야기

나는 아래의 코드를 실행하면 :

TypeError: The `text` argument passed to `__init__(text)` must be a string, not <class 'float'> 

방법 : df['sentiment'] = list(map(lambda tweet: TextBlob(tweet), df['text']))

내가 오류를 감정 값을 얻기 위해 데이터 프레임의 각 행에 textBLob을 적용합니까?

답변

1

당신은 .apply를 사용할 수 있습니다

df['sentiment'] = df['text'].apply(lambda tweet: TextBlob(tweet).sentiment) 

감정의 형식은 심리 (극성, 주관)의 namedtuple을 반환합니다.

그러나 df['text']의 각 행은 문자열 형식으로되어 있습니까? 그렇지 않다면 TextBlob로 텍스트를 처리 할 수없는 경우 None을 반환하려고 시도 할 수 있습니다.

def sentiment_calc(text): 
    try: 
     return TextBlob(text).sentiment 
    except: 
     return None 

df['sentiment'] = df['text'].apply(sentiment_calc) 
+0

감사합니다. – user2585048