Python Pandas - Basic Functionality
Pandas - Basic Functionality
Section titled “Pandas - Basic Functionality”Now that we understand how to create Pandas Series and DataFrames, let’s explore some fundamental attributes and methods used to inspect and understand these data structures. We’ll focus primarily on DataFrames due to their prevalence in data analysis.
Series Basic Attributes and Methods
Section titled “Series Basic Attributes and Methods”Here are some essential attributes and methods for Pandas Series:
| Attribute/Method | Description |
|---|---|
.index / .axes | Returns the index (axis labels) of the Series. .axes returns a list containing the index. |
.dtype | Returns the data type (dtype) of the elements in the Series. |
.empty | Returns True if the Series is empty (has zero elements), False otherwise. |
.ndim | Returns the number of dimensions (always 1 for a Series). |
.size | Returns the total number of elements in the Series. |
.shape | Returns a tuple representing the dimensionality (e.g., (n,) for a Series of length n). |
.values / .to_numpy() | Returns the Series data as a NumPy array. .to_numpy() is the recommended modern method. |
.head(n=5) | Returns the first n elements (default is 5). |
.tail(n=5) | Returns the last n elements (default is 5). |
Let’s create a sample Series and demonstrate these:
import pandas as pdimport numpy as np
# Create a sample Seriess = pd.Series(np.random.randn(5), index=['a', 'b', 'c', 'd', 'e'], name='SampleData')print("Sample Series:")print(s)Output (values will vary):
Sample Series:a 0.967853b -0.148368c -1.395906d -1.758394e 0.562421Name: SampleData, dtype: float64.index / .axes
Section titled “.index / .axes”print("\nIndex:", s.index)print("Axes:", s.axes)Output:
Index: Index(['a', 'b', 'c', 'd', 'e'], dtype='object')Axes: [Index(['a', 'b', 'c', 'd', 'e'], dtype='object')].dtype
Section titled “.dtype”print("\nDtype:", s.dtype)Output:
Dtype: float64.empty
Section titled “.empty”print("\nIs empty?", s.empty)print("Is pd.Series(dtype='float64').empty empty?", pd.Series(dtype='float64').empty)Output:
Is empty? FalseIs pd.Series(dtype='float64').empty empty? Trueprint("\nNumber of dimensions:", s.ndim)Output:
Number of dimensions: 1.size / .shape
Section titled “.size / .shape”print("\nSize (number of elements):", s.size)print("Shape (dimensionality tuple):", s.shape)Output:
Size (number of elements): 5Shape (dimensionality tuple): (5,).values / .to_numpy()
Section titled “.values / .to_numpy()”print("\nValues (as NumPy array using .values):")print(s.values)print("\nValues (as NumPy array using .to_numpy()):")print(s.to_numpy())Output (values will match the Series):
Values (as NumPy array using .values):[ 0.967853 -0.148368 -1.395906 -1.758394 0.562421 ]
Values (as NumPy array using .to_numpy()):[ 0.967853 -0.148368 -1.395906 -1.758394 0.562421 ]Note: .to_numpy() is generally preferred over .values in modern Pandas for clarity and consistency.
.head() & .tail()
Section titled “.head() & .tail()”print("\nFirst 2 elements (.head(2)):")print(s.head(2))print("\nLast 2 elements (.tail(2)):")print(s.tail(2))Output:
First 2 elements (.head(2)):a 0.967853b -0.148368Name: SampleData, dtype: float64
Last 2 elements (.tail(2)):d -1.758394e 0.562421Name: SampleData, dtype: float64DataFrame Basic Attributes and Methods
Section titled “DataFrame Basic Attributes and Methods”DataFrames share many attributes with Series but also have specifics related to their 2D nature:
| Attribute/Method | Description |
|---|---|
.T | Transposes the DataFrame (swaps rows and columns). |
.index | Returns the index (row labels) of the DataFrame. |
.columns | Returns the column labels of the DataFrame. |
.axes | Returns a list containing the index (axis 0) and columns (axis 1). |
.dtypes | Returns a Series containing the data type of each column. |
.empty | Returns True if the DataFrame is empty (0 rows or 0 columns). |
.ndim | Returns the number of dimensions (always 2 for a DataFrame). |
.size | Returns the total number of elements (rows * columns). |
.shape | Returns a tuple representing the dimensionality (rows, columns). |
.values / .to_numpy() | Returns the DataFrame data as a 2D NumPy array. .to_numpy() is recommended. |
.head(n=5) | Returns the first n rows. |
.tail(n=5) | Returns the last n rows. |
.info() | Prints a concise summary including index dtype, column dtypes, non-null values, and memory usage. |
Let’s create a sample DataFrame:
import pandas as pdimport numpy as np
# Create a dictionary for the DataFramedata = { 'Name': pd.Series(['Tom', 'James', 'Ricky', 'Vin', 'Steve', 'Smith', 'Jack']), 'Age': pd.Series([25, 26, 25, 23, 30, 29, 23]), 'Rating': pd.Series([4.23, 3.24, 3.98, 2.56, 3.20, 4.6, 3.8])}
# Create the DataFramedf = pd.DataFrame(data)print("Sample DataFrame:")print(df)Output:
Sample DataFrame: Name Age Rating0 Tom 25 4.231 James 26 3.242 Ricky 25 3.983 Vin 23 2.564 Steve 30 3.205 Smith 29 4.606 Jack 23 3.80.T (Transpose)
Section titled “.T (Transpose)”print("\nTransposed DataFrame (.T):")print(df.T)Output:
Transposed DataFrame (.T): 0 1 2 3 4 5 6Name Tom James Ricky Vin Steve Smith JackAge 25 26 25 23 30 29 23Rating 4.23 3.24 3.98 2.56 3.2 4.6 3.8.index, .columns, .axes
Section titled “.index, .columns, .axes”print("\nIndex (Row Labels):", df.index)print("Column Labels:", df.columns)print("Axes (List of Index, Columns):", df.axes)Output:
Index (Row Labels): RangeIndex(start=0, stop=7, step=1)Column Labels: Index(['Name', 'Age', 'Rating'], dtype='object')Axes (List of Index, Columns): [RangeIndex(start=0, stop=7, step=1), Index(['Name', 'Age', 'Rating'], dtype='object')].dtypes
Section titled “.dtypes”Returns a Series showing the data type of each column.
print("\nData types of each column (.dtypes):")print(df.dtypes)Output:
Data types of each column (.dtypes):Name objectAge int64Rating float64dtype: object.empty, .ndim, .size, .shape
Section titled “.empty, .ndim, .size, .shape”print("\nIs empty?", df.empty)print("Number of dimensions:", df.ndim)print("Size (total elements):", df.size)print("Shape (rows, columns):", df.shape)Output:
Is empty? FalseNumber of dimensions: 2Size (total elements): 21Shape (rows, columns): (7, 3).values / .to_numpy()
Section titled “.values / .to_numpy()”print("\nValues (as 2D NumPy array using .to_numpy()):")print(df.to_numpy())Output:
Values (as 2D NumPy array using .to_numpy()):[['Tom' 25 4.23] ['James' 26 3.24] ['Ricky' 25 3.98] ['Vin' 23 2.56] ['Steve' 30 3.2] ['Smith' 29 4.6] ['Jack' 23 3.8]].head() & .tail()
Section titled “.head() & .tail()”print("\nFirst 2 rows (.head(2)):")print(df.head(2))print("\nLast 2 rows (.tail(2)):")print(df.tail(2))Output:
First 2 rows (.head(2)): Name Age Rating0 Tom 25 4.231 James 26 3.24
Last 2 rows (.tail(2)): Name Age Rating5 Smith 29 4.66 Jack 23 3.8.info()
Section titled “.info()”Provides a quick overview of the DataFrame’s structure.
print("\nDataFrame Info (.info()):")df.info()Output:
DataFrame Info (.info()):<class 'pandas.core.frame.DataFrame'>RangeIndex: 7 entries, 0 to 6Data columns (total 3 columns): # Column Non-Null Count Dtype--- ------ -------------- ----- 0 Name 7 non-null object 1 Age 7 non-null int64 2 Rating 7 non-null float64dtypes: float64(1), int64(1), object(1)memory usage: 296.0+ bytes