.. 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_merge.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_merge.py: Merge 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 -0.381824 -0.615609 0.353890 0.073478
1 -0.627941 0.523366 0.763896 -0.858404
2 1.434160 0.695704 0.295234 0.947809
3 -0.318871 -0.470462 -0.500630 -1.794801
4 -1.053845 0.739370 -2.324050 0.441212
5 0.585735 1.022914 0.216315 0.836352
6 -0.770376 -2.096072 0.205964 -0.445026
7 -0.081697 -0.005139 0.267725 -0.149331
8 0.386357 0.231614 1.104906 2.569610
9 0.119584 -1.526672 0.358645 -0.081198


.. 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 -0.312961 0.144192
foo onetwotwoonethree -1.740804 -0.837059


.. GENERATED FROM PYTHON SOURCE LINES 45-46 .. code-block:: Python dataFrame.groupby(["A", "B"]).sum() .. raw:: html
C D
A B
bar one 0.096750 0.952195
three 0.157577 -0.027870
two -0.567287 -0.780133
foo one 0.448194 -0.132610
three 0.652331 0.879493
two -2.841329 -1.583942


.. _sphx_glr_download_11_demos_python_packages_pandas_demo_merge.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_merge.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_merge.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: demo_merge.zip `