30 Mins

3. DataFrame Foundations

DataFrame 2D Tabular Structure and Axes

A DataFrame is the primary data structure in Pandas, functioning as a 2D labeled array, much like a spreadsheet or SQL table. It consists of three components: the data (the values), the index (the row labels), and the columns (the header labels).

DataFrames are organized by axes. Axis 0 represents the index (rows) and Axis 1 represents the columns. Understanding these axes is critical for performing operations like drops, sums, or merges.

Operator Note: Think of Axis 0 as the vertical flow and Axis 1 as the horizontal flow across your data.
Python 3
import pandas as pd
data = {'Name': ['Alice', 'Bob'], 'Age': [25, 30]}
df = pd.DataFrame(data)
print(df.axes)

Index Labels vs Column Keys

In a DataFrame, rows are identified by an index, while columns are identified by keys. By default, Pandas creates a RangeIndex (0, 1, 2...), but you can set specific columns as the index for faster lookups.

Choosing between an index label and a column key depends on your retrieval logic. Use column keys for slicing features and index labels for selecting observations.

FeatureDescription
Index LabelsUnique identifiers for rows.
Column KeysNames associated with series of data.
Operator Note: Always check for duplicates in your index using .index.is_unique to prevent unexpected aggregation errors.
Python 3
df.set_index('Name', inplace=True)
print(df.loc['Alice'])

Inspecting Structures: head(), tail(), info(), describe()

Pandas provides powerful introspection methods to quickly summarize your data. head() and tail() allow you to preview the beginning and end of a dataset, which is essential for checking load integrity.

info() provides a concise summary of the DataFrame, including the number of non-null values and memory usage. describe() provides a statistical summary of numerical columns, calculating mean, count, min, max, and percentiles.

Operator Note: Always run df.info() first to identify missing data or incorrect data types immediately after loading.
Python 3
print(df.head(1))
print(df.info())
print(df.describe())

Checking Shape and Column Datatypes

The .shape property returns a tuple representing the dimensionality of the DataFrame (rows, columns). It is the fastest way to confirm that your data loading operation captured the expected number of records.

The .dtypes property returns a Series with the data type of each column. Ensuring columns are correctly typed (e.g., float64 for decimals, int64 for integers, object for strings) is crucial for accurate computations.

Operator Note: If a numeric column appears as an object, it likely contains hidden strings or empty values which must be cleaned before analysis.
Python 3
print(f'Rows and Columns: {df.shape}')
print(df.dtypes)

Knowledge Check

Ready to test your understanding of 3. DataFrame Foundations?

Back