.. only:: html

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

        Click :ref:`here <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 from lmfit.


.. code-block:: default

    import matplotlib.pyplot as plt
    import pandas as pd

    from lmfit.models import LorentzianModel








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


.. code-block:: default

    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'])





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

 Out:

 .. code-block:: none

    /usr/lib/python3/dist-packages/numpy/core/fromnumeric.py:58: FutureWarning: 
    The current behaviour of 'Series.argmax' is deprecated, use 'idxmax'
    instead.
    The behavior of 'argmax' will be corrected to return the positional
    maximum in the future. For now, use 'series.values.argmax' or
    'np.argmax(np.array(values))' to get the position of the maximum
    row.
      return bound(*args, **kwds)




and gives the plot and fitting results below:


.. code-block:: default

    result.plot_fit()
    plt.show()

    print(result.fit_report())



.. image:: /examples/images/sphx_glr_example_use_pandas_001.png
    :alt: Model(lorentzian)
    :class: sphx-glr-single-img


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

 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
    [[Variables]]
        amplitude:  39.1530829 +/- 0.62389698 (1.59%) (init = 50.7825)
        center:     9.22379520 +/- 0.01835869 (0.20%) (init = 9.3)
        sigma:      1.15503893 +/- 0.02603688 (2.25%) (init = 1.3)
        fwhm:       2.31007785 +/- 0.05207377 (2.25%) == '2.0000000*sigma'
        height:     10.7899514 +/- 0.17160472 (1.59%) == '0.3183099*amplitude/max(2.220446049250313e-16, sigma)'
    [[Correlations]] (unreported correlations are < 0.100)
        C(amplitude, sigma) =  0.709





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

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


.. _sphx_glr_download_examples_example_use_pandas.py:


.. only :: html

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



  .. 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-jupyter

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


.. only:: html

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

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