.. 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_create.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_create.py: Create and view an object ============================ .. GENERATED FROM PYTHON SOURCE LINES 5-10 .. code-block:: Python import pandas as pd import numpy as np .. GENERATED FROM PYTHON SOURCE LINES 12-13 **Create an object** .. GENERATED FROM PYTHON SOURCE LINES 13-15 .. code-block:: Python series = pd.Series([1, 3, 5, np.nan, 6, 8]) .. GENERATED FROM PYTHON SOURCE LINES 16-18 .. code-block:: Python dates = pd.date_range("20220501", periods=6) .. GENERATED FROM PYTHON SOURCE LINES 19-21 .. code-block:: Python dataFrame = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=list("ABCD")) .. GENERATED FROM PYTHON SOURCE LINES 22-33 .. code-block:: Python dataFrame2 = pd.DataFrame( { "A": 1.0, "B": pd.Timestamp("20220501"), "C": pd.Series(1, index=list(range(4)), dtype="float32"), "D": np.array([3] * 4, dtype="int32"), "E": pd.Categorical(["test", "train", "test", "train"]), "F": "foo", } ) .. GENERATED FROM PYTHON SOURCE LINES 34-35 **View an object** .. GENERATED FROM PYTHON SOURCE LINES 35-37 .. code-block:: Python dataFrame2.head() .. raw:: html
A B C D E F
0 1.0 2022-05-01 1.0 3 test foo
1 1.0 2022-05-01 1.0 3 train foo
2 1.0 2022-05-01 1.0 3 test foo
3 1.0 2022-05-01 1.0 3 train foo


.. GENERATED FROM PYTHON SOURCE LINES 38-40 .. code-block:: Python dataFrame2.tail() .. raw:: html
A B C D E F
0 1.0 2022-05-01 1.0 3 test foo
1 1.0 2022-05-01 1.0 3 train foo
2 1.0 2022-05-01 1.0 3 test foo
3 1.0 2022-05-01 1.0 3 train foo


.. GENERATED FROM PYTHON SOURCE LINES 41-43 .. code-block:: Python dataFrame2.dtypes .. rst-class:: sphx-glr-script-out .. code-block:: none A float64 B datetime64[s] C float32 D int32 E category F object dtype: object .. GENERATED FROM PYTHON SOURCE LINES 44-46 .. code-block:: Python dataFrame2.index .. rst-class:: sphx-glr-script-out .. code-block:: none Index([0, 1, 2, 3], dtype='int64') .. GENERATED FROM PYTHON SOURCE LINES 47-49 .. code-block:: Python dataFrame2.columns .. rst-class:: sphx-glr-script-out .. code-block:: none Index(['A', 'B', 'C', 'D', 'E', 'F'], dtype='object') .. GENERATED FROM PYTHON SOURCE LINES 50-52 .. code-block:: Python dataFrame2.describe() .. raw:: html
A B C D
count 4.0 4 4.0 4.0
mean 1.0 2022-05-01 00:00:00 1.0 3.0
min 1.0 2022-05-01 00:00:00 1.0 3.0
25% 1.0 2022-05-01 00:00:00 1.0 3.0
50% 1.0 2022-05-01 00:00:00 1.0 3.0
75% 1.0 2022-05-01 00:00:00 1.0 3.0
max 1.0 2022-05-01 00:00:00 1.0 3.0
std 0.0 NaN 0.0 0.0


.. GENERATED FROM PYTHON SOURCE LINES 53-54 Convert to numpy .. GENERATED FROM PYTHON SOURCE LINES 54-56 .. code-block:: Python dataFrame.to_numpy() .. rst-class:: sphx-glr-script-out .. code-block:: none array([[-0.48070149, 0.69640839, 0.91964576, -0.16073026], [-0.34969799, 1.00341675, -0.82853341, 0.78270195], [-1.79253563, 0.25743326, 0.58993783, -0.33844128], [ 1.79722667, -1.10740684, 0.48154367, 1.27495664], [ 0.38022496, -0.12789082, -0.85380203, 2.81773155], [ 0.25135383, 1.00941959, 1.31962369, -0.1835926 ]]) .. GENERATED FROM PYTHON SOURCE LINES 57-58 **Transpose, sorting data** .. GENERATED FROM PYTHON SOURCE LINES 58-60 .. code-block:: Python dataFrame.T .. raw:: html
2022-05-01 2022-05-02 2022-05-03 2022-05-04 2022-05-05 2022-05-06
A -0.480701 -0.349698 -1.792536 1.797227 0.380225 0.251354
B 0.696408 1.003417 0.257433 -1.107407 -0.127891 1.009420
C 0.919646 -0.828533 0.589938 0.481544 -0.853802 1.319624
D -0.160730 0.782702 -0.338441 1.274957 2.817732 -0.183593


.. GENERATED FROM PYTHON SOURCE LINES 61-63 .. code-block:: Python dataFrame.sort_index(axis=1, ascending=False) .. raw:: html
D C B A
2022-05-01 -0.160730 0.919646 0.696408 -0.480701
2022-05-02 0.782702 -0.828533 1.003417 -0.349698
2022-05-03 -0.338441 0.589938 0.257433 -1.792536
2022-05-04 1.274957 0.481544 -1.107407 1.797227
2022-05-05 2.817732 -0.853802 -0.127891 0.380225
2022-05-06 -0.183593 1.319624 1.009420 0.251354


.. GENERATED FROM PYTHON SOURCE LINES 64-65 .. code-block:: Python dataFrame.sort_values(by="B") .. raw:: html
A B C D
2022-05-04 1.797227 -1.107407 0.481544 1.274957
2022-05-05 0.380225 -0.127891 -0.853802 2.817732
2022-05-03 -1.792536 0.257433 0.589938 -0.338441
2022-05-01 -0.480701 0.696408 0.919646 -0.160730
2022-05-02 -0.349698 1.003417 -0.828533 0.782702
2022-05-06 0.251354 1.009420 1.319624 -0.183593


.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.030 seconds) .. _sphx_glr_download_11_demos_python_packages_pandas_demo_create.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_create.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_create.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: demo_create.zip `