.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "11_demos\python_packages\pandas\demo_operations.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_11_demos_python_packages_pandas_demo_operations.py: Operations ============= .. GENERATED FROM PYTHON SOURCE LINES 5-13 .. code-block:: Python import pandas as pd import numpy as np dates = pd.date_range("20220501", periods=6) dataFrame = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=list("ABCD")) .. GENERATED FROM PYTHON SOURCE LINES 15-16 **Statistics** .. GENERATED FROM PYTHON SOURCE LINES 16-18 .. code-block:: Python dataFrame.mean() .. rst-class:: sphx-glr-script-out .. code-block:: none A 0.510904 B 0.309009 C -0.114758 D -0.246521 dtype: float64 .. GENERATED FROM PYTHON SOURCE LINES 19-20 Mean value of axis ``1``: .. GENERATED FROM PYTHON SOURCE LINES 20-22 .. code-block:: Python dataFrame.mean(1) .. rst-class:: sphx-glr-script-out .. code-block:: none 2022-05-01 0.090335 2022-05-02 -0.014631 2022-05-03 -0.350706 2022-05-04 -0.214797 2022-05-05 0.602078 2022-05-06 0.575671 Freq: D, dtype: float64 .. GENERATED FROM PYTHON SOURCE LINES 23-25 .. code-block:: Python series = pd.Series([1, 3, 5, np.nan, 6, 8], index=dates).shift(2) .. GENERATED FROM PYTHON SOURCE LINES 26-28 .. code-block:: Python dataFrame.sub(series, axis="index") .. raw:: html
A B C D
2022-05-01 NaN NaN NaN NaN
2022-05-02 NaN NaN NaN NaN
2022-05-03 -0.284007 -0.906460 -2.257962 -1.954394
2022-05-04 -1.867116 -3.854271 -3.230730 -3.907071
2022-05-05 -4.482478 -3.683779 -4.677417 -4.748012
2022-05-06 NaN NaN NaN NaN


.. GENERATED FROM PYTHON SOURCE LINES 29-30 **Apply** .. GENERATED FROM PYTHON SOURCE LINES 30-32 .. code-block:: Python dataFrame.apply(np.cumsum) .. raw:: html
A B C D
2022-05-01 -0.063978 0.239891 0.216541 -0.031114
2022-05-02 0.064901 0.196255 0.275400 -0.233739
2022-05-03 0.780894 0.289795 -0.982562 -1.188133
2022-05-04 1.913777 -0.564475 -1.213292 -2.095204
2022-05-05 2.431299 0.751746 -0.890709 -1.843216
2022-05-06 3.065426 1.854051 -0.688547 -1.479128


.. GENERATED FROM PYTHON SOURCE LINES 33-35 .. code-block:: Python dataFrame.apply(lambda x: x.max() - x.min()) .. rst-class:: sphx-glr-script-out .. code-block:: none A 1.196861 B 2.170492 C 1.580545 D 1.318483 dtype: float64 .. GENERATED FROM PYTHON SOURCE LINES 36-37 **Histogramming** .. GENERATED FROM PYTHON SOURCE LINES 37-40 .. code-block:: Python series = pd.Series(np.random.randint(0, 7, size=10)) series.value_counts() .. rst-class:: sphx-glr-script-out .. code-block:: none 4 4 1 2 0 1 5 1 2 1 3 1 Name: count, dtype: int64 .. GENERATED FROM PYTHON SOURCE LINES 41-42 **String methods** .. GENERATED FROM PYTHON SOURCE LINES 42-44 .. code-block:: Python series = pd.Series(["A", "B", "C", "Aaba", "Baca", np.nan, "CABA", "dog", "cat"]) series.str.lower() .. rst-class:: sphx-glr-script-out .. code-block:: none 0 a 1 b 2 c 3 aaba 4 baca 5 NaN 6 caba 7 dog 8 cat dtype: object .. _sphx_glr_download_11_demos_python_packages_pandas_demo_operations.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_operations.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_operations.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: demo_operations.zip `