Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 900
Nadège Avatar asked Oct 31 '25 01:10

Nadège


1 Answers

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.

like image 128
kvorobiev Avatar answered Nov 01 '25 14:11

kvorobiev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!