View the top and bottom rows of a data frame:
import numpy as np
import pandas as pd
dates = pd.date_range('20190101', periods=8)
df = pd.DataFrame(np.random.randn(8, 4), index=dates, columns=list('PQRS'))
df.head()
df.tail(3)
Display the index, columns:
df.index
df.columns
DataFrame.to_numpy() gives a NumPy representation of the underlying data.
DataFrame.to_numpy() is fast and doesn’t require copying data.
df.to_numpy()
For df2, the DataFrame with multiple dtypes, DataFrame.to_numpy() is relatively expensive.
df2 = pd.DataFrame({'A': 1.,
'B': pd.Timestamp('20190102'),
'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'})
df2
df2.to_numpy()
Note: DataFrame.to_numpy() does not include the index or column labels in the output.
describe() function shows a quick statistic summary of your data:
df.describe()
Transposing data:
df.T
Sorting data by an axis:
df.sort_index(axis=1, ascending=False)
Sorting by values:
df.sort_values(by='Q')