.. 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_grouping.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_grouping.py: Grouping data ================ .. GENERATED FROM PYTHON SOURCE LINES 5-10 .. code-block:: Python import pandas as pd import numpy as np .. GENERATED FROM PYTHON SOURCE LINES 12-15 **Concat** Create a dataFrame .. GENERATED FROM PYTHON SOURCE LINES 15-17 .. code-block:: Python dataFrame = pd.DataFrame(np.random.randn(10, 4)) .. GENERATED FROM PYTHON SOURCE LINES 18-19 break in pieces .. GENERATED FROM PYTHON SOURCE LINES 19-21 .. code-block:: Python pieces = [dataFrame[:3], dataFrame[3:7], dataFrame[7:]] .. GENERATED FROM PYTHON SOURCE LINES 22-24 .. code-block:: Python pd.concat(pieces) .. raw:: html
0 1 2 3
0 2.005605 0.122321 0.314603 0.318486
1 -1.015193 0.074791 0.816450 -1.115685
2 -0.997419 -0.064948 0.973292 0.969946
3 1.281346 0.154516 -3.354396 -0.940462
4 -0.063201 0.225435 0.350171 1.603465
5 0.654310 0.085126 0.021388 -0.631416
6 1.328035 0.053975 0.672767 -1.708392
7 -0.512168 -0.044290 0.993882 -0.127941
8 1.000896 -0.096002 -0.327849 2.242147
9 0.407832 -0.135254 1.369629 0.255078


.. GENERATED FROM PYTHON SOURCE LINES 25-26 **Join** .. GENERATED FROM PYTHON SOURCE LINES 26-29 .. code-block:: Python left = pd.DataFrame({"key": ["foo", "foo"], "lval": [1, 2]}) right = pd.DataFrame({"key": ["foo", "foo"], "rval": [4, 5]}) .. GENERATED FROM PYTHON SOURCE LINES 30-32 .. code-block:: Python pd.merge(left, right, on="key") .. raw:: html
key lval rval
0 foo 1 4
1 foo 1 5
2 foo 2 4
3 foo 2 5


.. GENERATED FROM PYTHON SOURCE LINES 33-34 **Grouping** .. GENERATED FROM PYTHON SOURCE LINES 34-44 .. code-block:: Python dataFrame = pd.DataFrame( { "A": ["foo", "bar", "foo", "bar", "foo", "bar", "foo", "foo"], "B": ["one", "one", "two", "three", "two", "two", "one", "three"], "C": np.random.randn(8), "D": np.random.randn(8), } ) dataFrame.groupby("A").sum() .. raw:: html
B C D
A
bar onethreetwo 2.097917 0.999664
foo onetwotwoonethree 2.141605 0.976511


.. GENERATED FROM PYTHON SOURCE LINES 45-46 .. code-block:: Python dataFrame.groupby(["A", "B"]).sum() .. raw:: html
C D
A B
bar one 0.048469 0.013294
three 0.631859 -0.183019
two 1.417589 1.169390
foo one 1.114664 1.950852
three -0.457640 -2.412908
two 1.484580 1.438567


.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.011 seconds) .. _sphx_glr_download_11_demos_python_packages_pandas_demo_grouping.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_grouping.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_grouping.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: demo_grouping.zip `