From fd9d4e72cde1ef2229b6a856dec3383828a40403 Mon Sep 17 00:00:00 2001 From: Mortada Mehyar Date: Sat, 11 Apr 2015 11:47:02 -0700 Subject: [PATCH] DOC: add more examples to StringMethods on Index --- doc/source/text.rst | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/doc/source/text.rst b/doc/source/text.rst index ee91ea3c166b6..f417f56f51fbc 100644 --- a/doc/source/text.rst +++ b/doc/source/text.rst @@ -37,6 +37,32 @@ the equivalent (scalar) built-in string methods: idx.str.lstrip() idx.str.rstrip() +The string methods on Index are especially useful for cleaning up or +transforming DataFrame columns. For instance, you may have columns with +leading or trailing whitespace: + +.. ipython:: python + + df = DataFrame(randn(3, 2), columns=[' Column A ', ' Column B '], + index=range(3)) + df + +Since ``df.columns`` is an Index object, we can use the ``.str`` accessor + +.. ipython:: python + + df.columns.str.strip() + df.columns.str.lower() + +These string methods can then be used to clean up the columns as needed. +Here we are removing leading and trailing whitespaces, lowercasing all names, +and replacing any remaining whitespaces with underscores: + +.. ipython:: python + + df.columns = df.columns.str.strip().str.lower().str.replace(' ', '_') + df + Splitting and Replacing Strings -------------------------------