Note
Click here to download the full example code
12.3.10.4.8. 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[ns]
C float32
D int32
E category
F object
dtype: object
dataFrame2.index
Int64Index([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([[ 2.51194555, -1.80634016, -0.63091148, 0.57827393],
[-0.15524733, -0.92884337, -0.67329603, -0.16581944],
[ 0.13049717, 0.39749269, -0.02893293, 1.14751273],
[ 0.77122503, -0.74610714, 1.02496218, 0.06675625],
[ 2.0653632 , 1.08194213, -0.13582842, 0.50800546],
[-1.08819937, 1.69327597, 0.28704203, -0.38299148]])
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.033 seconds)