Skip to content

DOC: add more examples to StringMethods on Index #9858

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions doc/source/text.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------------------------------

Expand Down