You shall use a regular expression :
import re
trim_function = lambda x : re.findall("^\s*(.*?)\s*$",str(x))[0]
To explain a bit :
The character ^
represents the beginning of the string, and $
is the end of your string ; so that your expression will find exactly 1 match.
\s
represents any whitespace character. So \s*
is any sequence (even empty) of whitespaces.
.*?
is any sequence of any character. I could not explain precisely why, but the ?
sign let this experrsion be less greedy than \s*
so that the whitespaces will be counted outside the parenthesis.
Finally, the parethesis (...)
means that you are interseted in the substring(s) inside of them : the expression trimmed.
As re.findall
provides a list of matching substrings, we have to select the first element.
Now, for a DataFrame :
df = pd.DataFrame([' 164', '164', '164 ', ' 164 '])
df.applymap(trim_function)
For a Series
df = pd.Series([' 164', '164', '164 ', ' 164 '])
df.apply(trim_function)
For an Index
df = pd.Index([' 164', '164', '164 ', ' 164 '])
df.map(trim_function)
edit : Forgot : if you don't want to remove spaces at the end of each string, simply use the pattern "^\s*(.*?)"
.