I have a data frame of 15000 record which has text column (column name = clean) as a list, please refer below
I need to find the minimum value in each row and add as a new column called min
def find_min(x): x = min(x) return(x)
I tried to pass the above function
df1['min'] = df1['clean'].map(find_min)
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):
def find_min(x): try: return min(x) except ValueError: return np.nan df1['min'] = df1['clean'].map(find_min)
Another way is to skip the function and define this inline:
df1['min'] = df1['clean'].map(lambda x: min(x) if len(x) else np.nan)
You can also do this using a list comprehension, which is quite fast:
df1['min'] = [find_min(x) for x in df1['clean']]
or,
df1['min'] = [min(x) if len(x) else np.nan for x in df1['clean']]