Strip / trim all strings of a dataframe Strip / trim all strings of a dataframe python python

Strip / trim all strings of a dataframe


You can use DataFrame.select_dtypes to select string columns and then apply function str.strip.

Notice: Values cannot be types like dicts or lists, because their dtypes is object.

df_obj = df.select_dtypes(['object'])print (df_obj)0    a  1    c  df[df_obj.columns] = df_obj.apply(lambda x: x.str.strip())print (df)   0   10  a  101  c   5

But if there are only a few columns use str.strip:

df[0] = df[0].str.strip()


Money Shot

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)

Full Example

A more complete example:

import pandas as pddef 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 elementsdf = pd.DataFrame([['  a  ', 10], ['  c  ', 5]])df = trim_all_columns(df)print(df)>>>   0   10  a  101  c   5

Working Example

Here's a working example hosted by trinket:https://trinket.io/python3/e6ab7fb4ab


You can try:

df[0] = df[0].str.strip()

or more specifically for all string columns

non_numeric_columns = list(set(df.columns)-set(df._get_numeric_data().columns))df[non_numeric_columns] = df[non_numeric_columns].apply(lambda x : str(x).strip())