Skip to content
Advertisement

Applying custom function to a column of lists in pandas, how to handle exceptions?

I have a data frame of 15000 record which has text column (column name = clean) as a list, please refer below

enter image description here

I need to find the minimum value in each row and add as a new column called min

JavaScript

I tried to pass the above function

JavaScript

Getting below error

ValueError: min() arg is an empty sequence

It seems there is an empty list, how to address this as I am having 15000 records Please advise

Advertisement

Answer

Since we have a list of columns, let us try handling errors in your function using try/except (EAFP pattern):

JavaScript

Another way is to skip the function and define this inline:

JavaScript

You can also do this using a list comprehension, which is quite fast:

JavaScript

or,

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