.. 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_selection.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_selection.py: Selection of data ==================== .. GENERATED FROM PYTHON SOURCE LINES 4-12 .. code-block:: Python import numpy as np import pandas as pd dates = pd.date_range("20220501", periods=6) dataFrame = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=list("ABCD")) .. GENERATED FROM PYTHON SOURCE LINES 14-15 **Getting data** .. GENERATED FROM PYTHON SOURCE LINES 15-17 .. code-block:: Python dataFrame["A"] .. rst-class:: sphx-glr-script-out .. code-block:: none 2022-05-01 0.973799 2022-05-02 0.155131 2022-05-03 0.445839 2022-05-04 -0.613454 2022-05-05 -0.776714 2022-05-06 -0.600595 Freq: D, Name: A, dtype: float64 .. GENERATED FROM PYTHON SOURCE LINES 18-20 .. code-block:: Python dataFrame[0:3] .. raw:: html
A B C D
2022-05-01 0.973799 -1.856910 1.102405 1.448620
2022-05-02 0.155131 -0.582155 -0.230280 0.691659
2022-05-03 0.445839 -0.988124 0.265889 -1.048844


.. GENERATED FROM PYTHON SOURCE LINES 21-23 .. code-block:: Python dataFrame["20220501":"20220502"] .. raw:: html
A B C D
2022-05-01 0.973799 -1.856910 1.102405 1.448620
2022-05-02 0.155131 -0.582155 -0.230280 0.691659


.. GENERATED FROM PYTHON SOURCE LINES 24-25 **Selection by label** .. GENERATED FROM PYTHON SOURCE LINES 25-27 .. code-block:: Python dataFrame.loc[dates[0]] .. rst-class:: sphx-glr-script-out .. code-block:: none A 0.973799 B -1.856910 C 1.102405 D 1.448620 Name: 2022-05-01 00:00:00, dtype: float64 .. GENERATED FROM PYTHON SOURCE LINES 28-30 .. code-block:: Python dataFrame.loc[:, ["A", "B"]] .. raw:: html
A B
2022-05-01 0.973799 -1.856910
2022-05-02 0.155131 -0.582155
2022-05-03 0.445839 -0.988124
2022-05-04 -0.613454 -1.217677
2022-05-05 -0.776714 0.459753
2022-05-06 -0.600595 -0.796993


.. GENERATED FROM PYTHON SOURCE LINES 31-33 .. code-block:: Python dataFrame.loc["20220501":"20220502", ["A", "B"]] .. raw:: html
A B
2022-05-01 0.973799 -1.856910
2022-05-02 0.155131 -0.582155


.. GENERATED FROM PYTHON SOURCE LINES 34-36 .. code-block:: Python dataFrame.loc["20220501", ["A", "B"]] .. rst-class:: sphx-glr-script-out .. code-block:: none A 0.973799 B -1.856910 Name: 2022-05-01 00:00:00, dtype: float64 .. GENERATED FROM PYTHON SOURCE LINES 37-39 .. code-block:: Python dataFrame.loc[dates[0], "A"] .. rst-class:: sphx-glr-script-out .. code-block:: none 0.9737991449933912 .. GENERATED FROM PYTHON SOURCE LINES 40-42 .. code-block:: Python dataFrame.at[dates[0], "A"] .. rst-class:: sphx-glr-script-out .. code-block:: none 0.9737991449933912 .. GENERATED FROM PYTHON SOURCE LINES 43-44 **Selection by position** .. GENERATED FROM PYTHON SOURCE LINES 44-46 .. code-block:: Python dataFrame.iloc[3] .. rst-class:: sphx-glr-script-out .. code-block:: none A -0.613454 B -1.217677 C 0.144906 D 0.719465 Name: 2022-05-04 00:00:00, dtype: float64 .. GENERATED FROM PYTHON SOURCE LINES 47-49 .. code-block:: Python dataFrame.iloc[3:5, 0:2] .. raw:: html
A B
2022-05-04 -0.613454 -1.217677
2022-05-05 -0.776714 0.459753


.. GENERATED FROM PYTHON SOURCE LINES 50-52 .. code-block:: Python dataFrame.iloc[[1, 2, 4], [0, 2]] .. raw:: html
A C
2022-05-02 0.155131 -0.230280
2022-05-03 0.445839 0.265889
2022-05-05 -0.776714 0.131384


.. GENERATED FROM PYTHON SOURCE LINES 53-55 .. code-block:: Python dataFrame.iloc[1:3, :] .. raw:: html
A B C D
2022-05-02 0.155131 -0.582155 -0.230280 0.691659
2022-05-03 0.445839 -0.988124 0.265889 -1.048844


.. GENERATED FROM PYTHON SOURCE LINES 56-58 .. code-block:: Python dataFrame.iloc[:, 1:3] .. raw:: html
B C
2022-05-01 -1.856910 1.102405
2022-05-02 -0.582155 -0.230280
2022-05-03 -0.988124 0.265889
2022-05-04 -1.217677 0.144906
2022-05-05 0.459753 0.131384
2022-05-06 -0.796993 -0.133263


.. GENERATED FROM PYTHON SOURCE LINES 59-61 .. code-block:: Python dataFrame.iloc[1, 1] .. rst-class:: sphx-glr-script-out .. code-block:: none -0.5821550971365247 .. GENERATED FROM PYTHON SOURCE LINES 62-64 .. code-block:: Python dataFrame.iat[1, 1] .. rst-class:: sphx-glr-script-out .. code-block:: none -0.5821550971365247 .. GENERATED FROM PYTHON SOURCE LINES 65-66 **Boolean indexing** .. GENERATED FROM PYTHON SOURCE LINES 66-68 .. code-block:: Python dataFrame[dataFrame["A"] > 0] .. raw:: html
A B C D
2022-05-01 0.973799 -1.856910 1.102405 1.448620
2022-05-02 0.155131 -0.582155 -0.230280 0.691659
2022-05-03 0.445839 -0.988124 0.265889 -1.048844


.. GENERATED FROM PYTHON SOURCE LINES 69-71 .. code-block:: Python dataFrame[dataFrame > 0] .. raw:: html
A B C D
2022-05-01 0.973799 NaN 1.102405 1.448620
2022-05-02 0.155131 NaN NaN 0.691659
2022-05-03 0.445839 NaN 0.265889 NaN
2022-05-04 NaN NaN 0.144906 0.719465
2022-05-05 NaN 0.459753 0.131384 0.513463
2022-05-06 NaN NaN NaN 0.640226


.. GENERATED FROM PYTHON SOURCE LINES 72-75 .. code-block:: Python dataFrame2 = dataFrame.copy() dataFrame2["E"] = ["one", "one", "two", "three", "four", "three"] .. GENERATED FROM PYTHON SOURCE LINES 76-78 .. code-block:: Python dataFrame2[dataFrame2["E"].isin(["two", "four"])] .. raw:: html
A B C D E
2022-05-03 0.445839 -0.988124 0.265889 -1.048844 two
2022-05-05 -0.776714 0.459753 0.131384 0.513463 four


.. GENERATED FROM PYTHON SOURCE LINES 79-80 **Setting data** .. GENERATED FROM PYTHON SOURCE LINES 80-83 .. code-block:: Python series = pd.Series([1, 2, 3, 4, 5, 6], index=pd.date_range("20130102", periods=6)) dataFrame["F"] = series .. GENERATED FROM PYTHON SOURCE LINES 84-86 .. code-block:: Python dataFrame.at[dates[0], "A"] = 0 .. GENERATED FROM PYTHON SOURCE LINES 87-89 .. code-block:: Python dataFrame.iat[0, 1] = 0 .. GENERATED FROM PYTHON SOURCE LINES 90-92 .. code-block:: Python dataFrame.loc[:, "D"] = np.array([5] * len(dataFrame)) .. GENERATED FROM PYTHON SOURCE LINES 93-95 .. code-block:: Python dataFrame2 = dataFrame.copy() dataFrame2[dataFrame2 > 0] = -dataFrame2 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.019 seconds) .. _sphx_glr_download_11_demos_python_packages_pandas_demo_selection.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_selection.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_selection.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: demo_selection.zip `