Skip to content
Advertisement

How to use exception handling in pandas while using a function

I have the following dataframe:

JavaScript

I am attempting to use langdetect to detect the language of the text elements in column y.

This is the code I have used for that purpose:

JavaScript

Unfortunately, there are non-textual elements (including blanks, symbols, numbers and combinations of these) involved in this column, so I get the following traceback:

JavaScript

Is there a way I can employ exception handling so the detect function from the langdetect library may be used for those appropriate text elements?

Advertisement

Answer

So, given the following dataframe:

JavaScript

And, for the purpose of the answer, these mocked exception and function:

JavaScript

You can skip rows (row 1 here) in which “y” has non-textual elements, like this:

JavaScript

And so:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement