Cleaning the values of a multitype data frame in python/pandas, I want to trim the strings. I am currently doing it in two instructions :
import pandas as pd
Here's a compact version of using applymap with a straightforward lambda expression to call strip only when the value is of a string type:
df.applymap(lambda x: x.strip() if isinstance(x, str) else x)
A more complete example:
import pandas as pd
def trim_all_columns(df):
"""
Trim whitespace from ends of each value across all series in dataframe
"""
trim_strings = lambda x: x.strip() if isinstance(x, str) else x
return df.applymap(trim_strings)
# simple example of trimming whitespace from data elements
df = pd.DataFrame([[' a ', 10], [' c ', 5]])
df = trim_all_columns(df)
print(df)
>>>
0 1
0 a 10
1 c 5
Here's a working example hosted by trinket: https://trinket.io/python3/e6ab7fb4ab