.. 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_reshaping.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_reshaping.py: Reshaping data ================= .. GENERATED FROM PYTHON SOURCE LINES 5-22 .. code-block:: Python import pandas as pd import numpy as np tuples = list( zip( *[ ["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"], ["one", "two", "one", "two", "one", "two", "one", "two"], ] ) ) index = pd.MultiIndex.from_tuples(tuples, names=["first", "second"]) dataFrame = pd.DataFrame(np.random.randn(8, 2), index=index, columns=["A", "B"]) dataFrame2 = dataFrame[:4] .. GENERATED FROM PYTHON SOURCE LINES 24-25 **Stack** .. GENERATED FROM PYTHON SOURCE LINES 25-27 .. code-block:: Python stacked = dataFrame2.stack() .. GENERATED FROM PYTHON SOURCE LINES 28-30 .. code-block:: Python stacked.unstack() .. raw:: html
A B
first second
bar one -0.554887 0.638492
two 1.603665 1.977195
baz one -0.433228 0.564101
two 0.739366 -0.816024


.. GENERATED FROM PYTHON SOURCE LINES 31-33 .. code-block:: Python stacked.unstack(1) .. raw:: html
second one two
first
bar A -0.554887 1.603665
B 0.638492 1.977195
baz A -0.433228 0.739366
B 0.564101 -0.816024


.. GENERATED FROM PYTHON SOURCE LINES 34-36 .. code-block:: Python stacked.unstack(0) .. raw:: html
first bar baz
second
one A -0.554887 -0.433228
B 0.638492 0.564101
two A 1.603665 0.739366
B 1.977195 -0.816024


.. GENERATED FROM PYTHON SOURCE LINES 37-38 **Pivot tables** .. GENERATED FROM PYTHON SOURCE LINES 38-48 .. code-block:: Python dataFrame = pd.DataFrame( { "A": ["one", "one", "two", "three"] * 3, "B": ["A", "B", "C"] * 4, "C": ["foo", "foo", "foo", "bar", "bar", "bar"] * 2, "D": np.random.randn(12), "E": np.random.randn(12), } ) .. GENERATED FROM PYTHON SOURCE LINES 49-51 .. code-block:: Python pd.pivot_table(dataFrame, values="D", index=["A", "B"], columns=["C"]) .. raw:: html
C bar foo
A B
one A 0.427408 0.081922
B 0.732450 -0.332109
C -0.668688 -0.644812
three A 0.975939 NaN
B NaN 0.025847
C 0.445298 NaN
two A NaN 0.472312
B 1.680834 NaN
C NaN -1.301363


.. GENERATED FROM PYTHON SOURCE LINES 52-53 **Time series** .. GENERATED FROM PYTHON SOURCE LINES 53-57 .. code-block:: Python indexData = pd.date_range("1/5/2022", periods=100, freq="S") timestamps = pd.Series(np.random.randint(0, 500, len(indexData)), index=indexData) timestamps.resample("5Min").sum() .. rst-class:: sphx-glr-script-out .. code-block:: none C:\Workspace\itom_development_VS2019_Qt5.15.2_x64\itomProject\itom\demo\python_packages\pandas\demo_reshaping.py:53: FutureWarning: 'S' is deprecated and will be removed in a future version, please use 's' instead. 2022-01-05 24493 Freq: 5min, dtype: int32 .. GENERATED FROM PYTHON SOURCE LINES 58-60 .. code-block:: Python timeStempsUTC = timestamps.tz_localize("UTC") .. GENERATED FROM PYTHON SOURCE LINES 61-63 .. code-block:: Python timeStempsUTC.tz_convert("US/Eastern") .. rst-class:: sphx-glr-script-out .. code-block:: none 2022-01-04 19:00:00-05:00 351 2022-01-04 19:00:01-05:00 77 2022-01-04 19:00:02-05:00 269 2022-01-04 19:00:03-05:00 415 2022-01-04 19:00:04-05:00 47 ... 2022-01-04 19:01:35-05:00 357 2022-01-04 19:01:36-05:00 484 2022-01-04 19:01:37-05:00 162 2022-01-04 19:01:38-05:00 128 2022-01-04 19:01:39-05:00 340 Freq: s, Length: 100, dtype: int32 .. GENERATED FROM PYTHON SOURCE LINES 64-66 .. code-block:: Python ps = timestamps.to_period() .. GENERATED FROM PYTHON SOURCE LINES 67-69 .. code-block:: Python ps.to_timestamp() .. rst-class:: sphx-glr-script-out .. code-block:: none 2022-01-05 00:00:00 351 2022-01-05 00:00:01 77 2022-01-05 00:00:02 269 2022-01-05 00:00:03 415 2022-01-05 00:00:04 47 ... 2022-01-05 00:01:35 357 2022-01-05 00:01:36 484 2022-01-05 00:01:37 162 2022-01-05 00:01:38 128 2022-01-05 00:01:39 340 Freq: s, Length: 100, dtype: int32 .. GENERATED FROM PYTHON SOURCE LINES 70-73 .. code-block:: Python prng = pd.period_range("1990Q1", "2000Q4", freq="Q-NOV") ts = pd.Series(np.random.randn(len(prng)), prng) ts.index = (prng.asfreq("M", "e") + 1).asfreq("H", "s") + 9 .. rst-class:: sphx-glr-script-out .. code-block:: none C:\Workspace\itom_development_VS2019_Qt5.15.2_x64\itomProject\itom\demo\python_packages\pandas\demo_reshaping.py:72: FutureWarning: 'H' is deprecated and will be removed in a future version, please use 'h' instead. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.042 seconds) .. _sphx_glr_download_11_demos_python_packages_pandas_demo_reshaping.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_reshaping.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_reshaping.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: demo_reshaping.zip `