Note
Go to the end to download the full example code.
12.3.10.4.2. Create and view an object#
import pandas as pd
import numpy as np
Create an object
series = pd.Series([1, 3, 5, np.nan, 6, 8])
dates = pd.date_range("20220501", periods=6)
dataFrame = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=list("ABCD"))
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",
}
)
View an object
dataFrame2.head()
dataFrame2.tail()
dataFrame2.dtypes
A float64
B datetime64[s]
C float32
D int32
E category
F object
dtype: object
dataFrame2.index
Index([0, 1, 2, 3], dtype='int64')
dataFrame2.columns
Index(['A', 'B', 'C', 'D', 'E', 'F'], dtype='object')
dataFrame2.describe()
Convert to numpy
dataFrame.to_numpy()
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 ]])
Transpose, sorting data
dataFrame.T
dataFrame.sort_index(axis=1, ascending=False)
dataFrame.sort_values(by="B")
Total running time of the script: (0 minutes 0.030 seconds)