

.. _sphx_glr_examples_dataframe.py:


Extracting data and labels from a :class:`~pandas.DataFrame`
============================================================

:class:`~pandas.DataFrame`\s can be used similarly to any other kind of input.
Here, we generate a scatter plot using two columns and label the points using
a third column.




.. image:: /examples/images/sphx_glr_dataframe_001.png
    :align: center





.. code-block:: python


    from matplotlib import pyplot as plt
    import mplcursors
    from pandas import DataFrame


    df = DataFrame(
        [("Alice", 163, 54),
         ("Bob", 174, 67),
         ("Charlie", 177, 73),
         ("Diane", 168, 57)],
        columns=["name", "height", "weight"])

    df.plot.scatter("height", "weight")
    mplcursors.cursor().connect(
        "add", lambda sel: sel.annotation.set_text(df["name"][sel.target.index]))
    plt.show()

    # test: skip



.. only :: html

 .. container:: sphx-glr-footer


  .. container:: sphx-glr-download

     :download:`Download Python source code: dataframe.py <dataframe.py>`



  .. container:: sphx-glr-download

     :download:`Download Jupyter notebook: dataframe.ipynb <dataframe.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.readthedocs.io>`_
