
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "examples/example_use_pandas.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_examples_example_use_pandas.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_examples_example_use_pandas.py:


Fit with Data in a pandas DataFrame
===================================

Simple example demonstrating how to read in the data using ``pandas`` and
supply the elements of the ``DataFrame`` to lmfit.

.. GENERATED FROM PYTHON SOURCE LINES 9-13

.. code-block:: Python

    import pandas as pd

    from lmfit.models import LorentzianModel








.. GENERATED FROM PYTHON SOURCE LINES 14-15

read the data into a pandas DataFrame, and use the ``x`` and ``y`` columns:

.. GENERATED FROM PYTHON SOURCE LINES 15-22

.. code-block:: Python

    dframe = pd.read_csv('peak.csv')

    model = LorentzianModel()
    params = model.guess(dframe['y'], x=dframe['x'])

    result = model.fit(dframe['y'], params, x=dframe['x'])








.. GENERATED FROM PYTHON SOURCE LINES 23-24

and gives the fitting results:

.. GENERATED FROM PYTHON SOURCE LINES 24-26

.. code-block:: Python

    print(result.fit_report())





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    [[Model]]
        Model(lorentzian)
    [[Fit Statistics]]
        # fitting method   = leastsq
        # function evals   = 21
        # data points      = 101
        # variables        = 3
        chi-square         = 13.0737250
        reduced chi-square = 0.13340536
        Akaike info crit   = -200.496119
        Bayesian info crit = -192.650757
        R-squared          = 0.98351484
    [[Variables]]
        amplitude:  39.1530621 +/- 0.62389897 (1.59%) (init = 50.7825)
        center:     9.22379948 +/- 0.01835867 (0.20%) (init = 9.3)
        sigma:      1.15503770 +/- 0.02603721 (2.25%) (init = 1.3)
        fwhm:       2.31007541 +/- 0.05207442 (2.25%) == '2.0000000*sigma'
        height:     10.7899571 +/- 0.17160652 (1.59%) == '0.3183099*amplitude/max(1e-15, sigma)'
    [[Correlations]] (unreported correlations are < 0.100)
        C(amplitude, sigma) = +0.7087




.. GENERATED FROM PYTHON SOURCE LINES 27-28

and plot below:

.. GENERATED FROM PYTHON SOURCE LINES 28-29

.. code-block:: Python

    result.plot_fit()



.. image-sg:: /examples/images/sphx_glr_example_use_pandas_001.png
   :alt: Model(lorentzian)
   :srcset: /examples/images/sphx_glr_example_use_pandas_001.png, /examples/images/sphx_glr_example_use_pandas_001_3_00x.png 3.00x
   :class: sphx-glr-single-img






.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 0.307 seconds)


.. _sphx_glr_download_examples_example_use_pandas.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

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

    .. container:: sphx-glr-download sphx-glr-download-python

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

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: example_use_pandas.zip <example_use_pandas.zip>`


.. only:: html

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

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