pythontypeerrorlanguage-detection

How to detect language of a dataframe object?


I want to create a new column in my dataframe review giving the language of the column text which is of type object.

I try to convert to string and then use the detect function from langdetect but, there still a type error when I run the code.

I do not understand the problem lol

My code :

from langdetect import detect


review['langue'] = detect((review['text']).astype(str))

Actual result :

--------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)


TypeError: expected string or bytes-like object

Solution

  • If I correctly understood your question you needs

    from langdetect import detect
    review['langue'] = review['text'].apply(detect)
    

    detect function expect str as argument, not pd.Series. Instead, you should apply detect function to each element of review['text'] pd.Series.