Skip to content

Instantly share code, notes, and snippets.

@yeiichi
Created January 15, 2026 07:17
Show Gist options
  • Select an option

  • Save yeiichi/2bd7ef5347742b806fd98faaf9bf9c93 to your computer and use it in GitHub Desktop.

Select an option

Save yeiichi/2bd7ef5347742b806fd98faaf9bf9c93 to your computer and use it in GitHub Desktop.
Strip leading and trailing whitespace from DataFrame string cells, leaving other types unchanged
def strip_df(df):
"""Strip leading and trailing whitespace from DataFrame string cells, leaving other types unchanged."""
return df.map(lambda s: s.strip() if isinstance(s, str) else s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment