Use lambda function with if-else
, also added converting to intgers for correct max
:
f = lambda x : max(int(y) for y in x) if isinstance(x, list) else np.nan
df['C'] = df['B'].apply(f)
print (df)
A B C
0 54321 NaN NaN
1 it is 54322 [54322] 54322.0
2 is it 54323 or 4? [54323, 4] 54323.0
3 NaN NaN NaN
Or use Series.str.extractall
for MultiIndex
with convert to int
and using max
per first level:
df = pd.DataFrame({'A' : [54321, 'it is 54322', 'is it 54323 or 4?', np.NaN]})
df['C'] = df.A.astype(str).str.extractall('(\d+)').astype(int).max(level=0)
print (df)
A C
0 54321 54321.0
1 it is 54322 54322.0
2 is it 54323 or 4? 54323.0
3 NaN NaN
CLICK HERE to find out more related problems solutions.