DataFrames
A Pandas DataFrame is a two-dimensional table made of rows and columns. It is one of the most important structures in Pandas because real-world datasets are usually organized like tables.
A DataFrame is a table in Python.
If a Pandas Series is like one column, a DataFrame is like the complete table containing multiple columns. Each column can contain a different type of information.
What Is a DataFrame?
A DataFrame is a two-dimensional labeled data structure.
You can think of it like a spreadsheet:
Name Age City Raj 29 Hyderabad John 32 London Sarah 27 New York
Here we have:
- Rows representing individual people.
- Columns representing different pieces of information.
- Labels that help us identify the data.
Create a DataFrame
The easiest way to understand a DataFrame is to create one.
import pandas as pd
data = {
"Name": ["Raj", "John", "Sarah"],
"Age": [29, 32, 27],
"City": ["Hyderabad", "London", "New York"]
}
df = pd.DataFrame(data)
print(df)
Name Age City 0 Raj 29 Hyderabad 1 John 32 London 2 Sarah 27 New York
The dictionary keys become the column names.
The dictionary values become the data inside those columns.
Understand the DataFrame Structure
Look at the DataFrame:
Name Age City 0 Raj 29 Hyderabad 1 John 32 London 2 Sarah 27 New York
There are three important parts:
- Columns — Name, Age, City.
- Rows — each person's record.
- Index — 0, 1, 2.
Think of it like this
DataFrame │ ├── Index │ ├── 0 │ ├── 1 │ └── 2 │ ├── Name ├── Age └── City
The DataFrame combines multiple columns into one structured table.
Series vs DataFrame
You learned Series in the previous lesson. Now the relationship becomes important.
Series
scores = pd.Series([80, 90, 75])
A Series is one-dimensional — think of it as one column.
DataFrame
students = pd.DataFrame({
"Name": ["Raj", "John", "Sarah"],
"Score": [80, 90, 75]
})
A DataFrame is two-dimensional — think of it as a complete table.
In fact, each DataFrame column is a Pandas Series.
Access a Column
We can select a single column using its name.
print(df["Name"])
0 Raj 1 John 2 Sarah Name: Name, dtype: object
The result is a Series.
This is an important relationship:
DataFrame
↓
Select one column
↓
Series
Access Multiple Columns
We can select more than one column by passing a list of column names.
print(df[["Name", "City"]])
Name City 0 Raj Hyderabad 1 John London 2 Sarah New York
Notice the double square brackets:
df[["Name", "City"]]
The inner brackets contain the list of columns we want to select.
Access a Row With iloc
iloc allows us to access rows using their
integer position.
print(df.iloc[0])
Name Raj Age 29 City Hyderabad Name: 0, dtype: object
Index 0 means the first row.
The second row would be:
print(df.iloc[1])
Access a Specific Value
We can access a specific value by providing the row position and column position.
print(df.iloc[0, 1])
29
This means:
df.iloc[row, column] df.iloc[0, 1]
Row 0 is Raj and column 1 is
Age, so the result is 29.
Add a New Column
We can create a new column directly.
df["Score"] = [80, 90, 75] print(df)
Name Age City Score 0 Raj 29 Hyderabad 80 1 John 32 London 90 2 Sarah 27 New York 75
The new Score column was added to the
DataFrame.
Perform Calculations on Columns
DataFrame columns can be used in calculations.
For example, suppose we have product prices and quantities.
products = pd.DataFrame({
"Product": ["Laptop", "Mouse", "Keyboard"],
"Price": [900, 25, 70],
"Quantity": [2, 5, 3]
})
products["Total"] = (
products["Price"] * products["Quantity"]
)
print(products)
Product Price Quantity Total 0 Laptop 900 2 1800 1 Mouse 25 5 125 2 Keyboard 70 3 210
Pandas performed the calculation for every row.
This type of column calculation is extremely common when working with real datasets.
Inspect a DataFrame
When you load a real dataset, the first thing you should do is inspect it.
head()
head() shows the first five rows by
default.
print(df.head())
shape
shape tells us how many rows and columns
exist.
print(df.shape)
(3, 4)
That means 3 rows and 4 columns.
See Column Names
We can see all column names using columns.
print(df.columns)
Index(['Name', 'Age', 'City', 'Score'], dtype='object')
This is useful when you are working with a dataset containing many columns and you need to know their exact names.
Check Data Types
Different columns can contain different types of data.
print(df.dtypes)
Name object Age int64 City object Score int64 dtype: object
For example:
int64represents integer numbers.objectcommonly represents text data.
Understanding data types becomes important when preparing data for Machine Learning.
DataFrames in AI
Imagine you have a Machine Learning dataset containing information about houses:
Area Bedrooms Age Price 1200 3 10 250000 1800 4 5 400000 900 2 20 180000 2200 4 3 500000
This is exactly the kind of data a DataFrame is good at representing.
Later, you might use Pandas to clean and prepare these columns before sending the data to a Machine Learning algorithm.
Simple Mental Model
Think about a spreadsheet
DataFrame
│
┌────────────┼────────────┐
↓ ↓ ↓
Name Age City
│ │ │
↓ ↓ ↓
Series Series Series
│
↓
One column
A DataFrame is made up of multiple Series columns aligned by their index.
A DataFrame is Pandas' main table structure.
A DataFrame contains rows, columns, and an index. You can create DataFrames from dictionaries, select columns, access rows, add new columns, perform calculations, and inspect the structure of your data. Most real-world Data Science and Machine Learning datasets are naturally represented as DataFrames.