.. _10min_tut_05_columns: {{ header }} .. ipython:: python import pandas as pd .. raw:: html
Data used for this tutorial:
How to create new columns derived from existing columns ------------------------------------------------------- .. image:: ../../_static/schemas/05_newcolumn_1.svg :align: center .. raw:: html .. note:: The calculation of the values is done **element-wise**. This means all values in the given column are multiplied by the value 1.882 at once. You do not need to use a loop to iterate each of the rows! .. image:: ../../_static/schemas/05_newcolumn_2.svg :align: center .. raw:: html Also other mathematical operators (``+``, ``-``, ``*``, ``/``,…) or logical operators (``<``, ``>``, ``==``,…) work element-wise. The latter was already used in the :ref:`subset data tutorial <10min_tut_03_subset>` to filter rows of a table using a conditional expression. If you need more advanced logic, you can use arbitrary Python code via :meth:`~DataFrame.apply`. .. raw:: html The mapping should not be restricted to fixed names only, but can be a mapping function as well. For example, converting the column names to lowercase letters can be done using a function as well: .. ipython:: python air_quality_renamed = air_quality_renamed.rename(columns=str.lower) air_quality_renamed.head() .. raw:: html
To user guide Details about column or row label renaming is provided in the user guide section on :ref:`renaming labels `. .. raw:: html
.. raw:: html

REMEMBER

- Create a new column by assigning the output to the DataFrame with a new column name in between the ``[]``. - Operations are element-wise, no need to loop over rows. - Use ``rename`` with a dictionary or function to rename row labels or column names. .. raw:: html
.. raw:: html
To user guide The user guide contains a separate section on :ref:`column addition and deletion `. .. raw:: html