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
JavaScript
x
5
1
def find_min(x):
2
x = min(x)
3
return(x)
4
5
I tried to pass the above function
JavaScript
1
3
1
df1['min'] = df1['clean'].map(find_min)
2
3
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
1
8
1
def find_min(x):
2
try:
3
return min(x)
4
except ValueError:
5
return np.nan
6
7
df1['min'] = df1['clean'].map(find_min)
8
Another way is to skip the function and define this inline:
JavaScript
1
2
1
df1['min'] = df1['clean'].map(lambda x: min(x) if len(x) else np.nan)
2
You can also do this using a list comprehension, which is quite fast:
JavaScript
1
2
1
df1['min'] = [find_min(x) for x in df1['clean']]
2
or,
JavaScript
1
2
1
df1['min'] = [min(x) if len(x) else np.nan for x in df1['clean']]
2