.. _10min_tut_10_text: {{ header }} .. ipython:: python import pandas as pd .. raw:: html
Data used for this tutorial:
How to manipulate textual data ------------------------------ .. raw:: html Similar to datetime objects in the :ref:`time series tutorial <10min_tut_09_timeseries>` having a ``dt`` accessor, a number of specialized string methods are available when using the ``str`` accessor. These methods have in general matching names with the equivalent built-in string methods for single elements, but are applied element-wise (remember :ref:`element-wise calculations <10min_tut_05_columns>`?) on each of the values of the columns. .. raw:: html .. raw:: html
To user guide More information on extracting parts of strings is available in the user guide section on :ref:`splitting and replacing strings `. .. raw:: html
.. raw:: html .. note:: More powerful extractions on strings are supported, as the :meth:`Series.str.contains` and :meth:`Series.str.extract` methods accept `regular expressions `__, but out of scope of this tutorial. .. raw:: html
To user guide More information on extracting parts of strings is available in the user guide section on :ref:`string matching and extracting `. .. raw:: html
.. raw:: html .. raw:: html .. warning:: There is also a :meth:`~Series.str.replace` method available to replace a specific set of characters. However, when having a mapping of multiple values, this would become: :: titanic["Sex_short"] = titanic["Sex"].str.replace("female", "F") titanic["Sex_short"] = titanic["Sex_short"].str.replace("male", "M") This would become cumbersome and easily lead to mistakes. Just think (or try out yourself) what would happen if those two statements are applied in the opposite order… .. raw:: html

REMEMBER

- String methods are available using the ``str`` accessor. - String methods work element-wise and can be used for conditional indexing. - The ``replace`` method is a convenient method to convert values according to a given dictionary. .. raw:: html
.. raw:: html
To user guide A full overview is provided in the user guide pages on :ref:`working with text data `. .. raw:: html