I am running the following function but somehow struggling to have it take the length condition into account (the if part). It simply runs the first part if the function only:
stringDataFrame.apply(lambda x: x.str.replace(r'[^0-9]', '') if (len(x) >= 7) else x)
it somehow only runs the x.str.replace(r'[^0-9]', '')
part for some reason, what am I doing wrong here i have been stuck.
You can use
applymap
when you need to work on each value separately, becauseapply
works withall column
(Series
).Then instead of using
str.replace
, usere.sub
which works nicer for regexs:Sample: