Skip to content

Python 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.

Here are some essential attributes and methods for Pandas Series:

Attribute/MethodDescription
.index / .axesReturns the index (axis labels) of the Series. .axes returns a list containing the index.
.dtypeReturns the data type (dtype) of the elements in the Series.
.emptyReturns True if the Series is empty (has zero elements), False otherwise.
.ndimReturns the number of dimensions (always 1 for a Series).
.sizeReturns the total number of elements in the Series.
.shapeReturns 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 pd
import numpy as np
# Create a sample Series
s = 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.967853
b -0.148368
c -1.395906
d -1.758394
e 0.562421
Name: SampleData, dtype: float64
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')]
print("\nDtype:", s.dtype)

Output:

Dtype: float64
print("\nIs empty?", s.empty)
print("Is pd.Series(dtype='float64').empty empty?", pd.Series(dtype='float64').empty)

Output:

Is empty? False
Is pd.Series(dtype='float64').empty empty? True
print("\nNumber of dimensions:", s.ndim)

Output:

Number of dimensions: 1
print("\nSize (number of elements):", s.size)
print("Shape (dimensionality tuple):", s.shape)

Output:

Size (number of elements): 5
Shape (dimensionality tuple): (5,)
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.

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.967853
b -0.148368
Name: SampleData, dtype: float64
Last 2 elements (.tail(2)):
d -1.758394
e 0.562421
Name: SampleData, dtype: float64

DataFrames share many attributes with Series but also have specifics related to their 2D nature:

Attribute/MethodDescription
.TTransposes the DataFrame (swaps rows and columns).
.indexReturns the index (row labels) of the DataFrame.
.columnsReturns the column labels of the DataFrame.
.axesReturns a list containing the index (axis 0) and columns (axis 1).
.dtypesReturns a Series containing the data type of each column.
.emptyReturns True if the DataFrame is empty (0 rows or 0 columns).
.ndimReturns the number of dimensions (always 2 for a DataFrame).
.sizeReturns the total number of elements (rows * columns).
.shapeReturns 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 pd
import numpy as np
# Create a dictionary for the DataFrame
data = {
'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 DataFrame
df = pd.DataFrame(data)
print("Sample DataFrame:")
print(df)

Output:

Sample DataFrame:
Name Age Rating
0 Tom 25 4.23
1 James 26 3.24
2 Ricky 25 3.98
3 Vin 23 2.56
4 Steve 30 3.20
5 Smith 29 4.60
6 Jack 23 3.80
print("\nTransposed DataFrame (.T):")
print(df.T)

Output:

Transposed DataFrame (.T):
0 1 2 3 4 5 6
Name Tom James Ricky Vin Steve Smith Jack
Age 25 26 25 23 30 29 23
Rating 4.23 3.24 3.98 2.56 3.2 4.6 3.8
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')]

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 object
Age int64
Rating float64
dtype: object
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? False
Number of dimensions: 2
Size (total elements): 21
Shape (rows, columns): (7, 3)
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]]
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 Rating
0 Tom 25 4.23
1 James 26 3.24
Last 2 rows (.tail(2)):
Name Age Rating
5 Smith 29 4.6
6 Jack 23 3.8

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 6
Data 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 float64
dtypes: float64(1), int64(1), object(1)
memory usage: 296.0+ bytes