Pandas DataFrames
Learn how to create a DataFrame, organize data into rows and columns, inspect its structure, select columns, and perform basic calculations using practical datasets.
1. Learning Objectives
After completing this lesson, you will be able to:
- Explain what a Pandas DataFrame is.
- Create a DataFrame from dictionaries and lists.
- Understand DataFrame rows, columns, and indexes.
- Inspect dimensions, column names, and data types.
- Select individual and multiple columns.
- Understand the difference between a Series and a DataFrame.
- Perform calculations using DataFrame columns.
- Apply DataFrames to a practical student dataset.
2. What Is a Pandas DataFrame?
A DataFrame is a two-dimensional labelled data structure provided by Pandas. It organizes information into rows and columns, similar to a spreadsheet or a database table.
Each row can represent a record, while each column represents a field. For example, a student dataset may contain student names, marks, and cities.
Figure 1: A DataFrame organizes records into labelled columns and indexed rows.
3. Creating a DataFrame from a Dictionary
One of the most common ways to create a DataFrame is by
passing a dictionary to pd.DataFrame().
Dictionary keys become column names, and their values
provide the column data.
import pandas as pd
data = {
"Name": ["Samadhan", "Pradnya", "Samiksha"],
"Marks": [85, 92, 78],
"City": ["Pune", "Mumbai", "Nashik"]
}
df = pd.DataFrame(data)
print(df)
Output:
Name Marks City 0 Samadhan 85 Pune 1 Pradnya 92 Mumbai 2 Samiksha 78 Nashik
Understanding the Code
import pandas as pdimports Pandas.datastores the source information in a dictionary.pd.DataFrame(data)creates the DataFrame.print(df)displays the table.
4. Creating a DataFrame from a List of Dictionaries
A list of dictionaries is useful when each dictionary represents one record. Pandas combines the records into a single tabular structure.
import pandas as pd
students = [
{"Name": "Samadhan", "Marks": 85},
{"Name": "Pradnya", "Marks": 92},
{"Name": "Samiksha", "Marks": 78}
]
df = pd.DataFrame(students)
print(df)
Output:
Name Marks 0 Samadhan 85 1 Pradnya 92 2 Samiksha 78
In this example, each dictionary describes one student.
The keys Name and Marks become
columns in the resulting DataFrame.
5. Creating a DataFrame from a List of Lists
You can create a DataFrame from a list of lists by supplying
the column names through the columns parameter.
import pandas as pd
data = [
["Samadhan", 85],
["Pradnya", 92],
["Samiksha", 78]
]
df = pd.DataFrame(
data,
columns=["Name", "Marks"]
)
print(df)
Output:
Name Marks 0 Samadhan 85 1 Pradnya 92 2 Samiksha 78
Here, the first value in each inner list goes into the
Name column, and the second value goes into
the Marks column.
6. Understanding DataFrame Dimensions
DataFrame properties help you understand the size and structure of your dataset.
import pandas as pd
data = {
"Name": ["Samadhan", "Pradnya", "Samiksha"],
"Marks": [85, 92, 78],
"City": ["Pune", "Mumbai", "Nashik"]
}
df = pd.DataFrame(data)
print("Shape:", df.shape)
print("Rows:", len(df))
print("Columns:", len(df.columns))
print("Total cells:", df.size)
print("Dimensions:", df.ndim)
Output:
Shape: (3, 3) Rows: 3 Columns: 3 Total cells: 9 Dimensions: 2
| Property | Meaning | Example |
|---|---|---|
df.shape |
Number of rows and columns. | (3, 3) |
len(df) |
Number of rows. | 3 |
df.size |
Total number of cells. | 9 |
df.ndim |
Number of dimensions. | 2 |
7. Inspecting Columns and Data Types
Use columns to view column labels and
dtypes to inspect the data type of each column.
print("Column names:")
print(df.columns)
print("Data types:")
print(df.dtypes)
Example output:
Column names: Index(['Name', 'Marks', 'City'], dtype='object') Data types: Name object Marks int64 City object dtype: object
Text columns may use the object dtype,
while whole-number values may use an integer dtype such
as int64. The exact dtype can depend on the
values and environment.
Previewing DataFrame Rows
print(df.head(2)) print(df.tail(2))
head(2)displays the first two rows.tail(2)displays the last two rows.head()andtail()use five rows by default.
8. Selecting a Single Column
You can select a single column by placing its name inside square brackets.
print(df["Name"])
Output:
0 Samadhan 1 Pradnya 2 Samiksha Name: Name, dtype: object
Selecting one column with df["Name"] returns
a Pandas Series. The original row index labels are retained.
9. Selecting Multiple Columns
To select multiple columns, provide a list of column names inside the square brackets.
selected = df[["Name", "Marks"]] print(selected)
Output:
Name Marks 0 Samadhan 85 1 Pradnya 92 2 Samiksha 78
The result is a DataFrame containing only the requested columns. The row index is preserved.
| Expression | Result |
|---|---|
df["Name"] |
A Series. |
df[["Name"]] |
A one-column DataFrame. |
df[["Name", "Marks"]] |
A DataFrame with two columns. |
KeyError.
10. Creating a Calculated Column
A DataFrame can store the results of calculations in a new column. This is a common operation in data analysis.
import pandas as pd
data = {
"Name": ["Samadhan", "Pradnya", "Samiksha"],
"Python": [80, 95, 88],
"Pandas": [85, 92, 90]
}
df = pd.DataFrame(data)
df["Average"] = (
df["Python"] + df["Pandas"]
) / 2
print(df)
Output:
Name Python Pandas Average 0 Samadhan 80 85 82.5 1 Pradnya 95 92 93.5 2 Samiksha 88 90 89.0
Pandas performs the addition element by element and divides
each result by two. The calculated values are assigned to
the new Average column.
Calculate the Average of the Class
print(df["Average"].mean())
Output:
88.33333333333333
11. Practical Exercise
CIIT TRAINING INSTITUTE: Student Data Analysis
Create a DataFrame containing information about Samadhan, Pradnya, and Samiksha.
Complete the following tasks:
- Create columns for Name, Python, and Pandas.
- Display the complete DataFrame.
- Display only the Name column.
- Select the Name and Python columns.
- Display the DataFrame shape and data types.
- Create an Average column.
- Calculate the class average.
Practice Code
import pandas as pd
data = {
"Name": ["Samadhan", "Pradnya", "Samiksha"],
"Python": [80, 95, 88],
"Pandas": [85, 92, 90]
}
df = pd.DataFrame(data)
print(df)
print("\nName column:")
print(df["Name"])
print("\nSelected columns:")
print(df[["Name", "Python"]])
print("\nShape:", df.shape)
print("\nData types:")
print(df.dtypes)
df["Average"] = (
df["Python"] + df["Pandas"]
) / 2
print("\nAverage marks:")
print(df[["Name", "Average"]])
print("\nClass average:", df["Average"].mean())
Revision Questions
- What is a Pandas DataFrame?
- How can you create a DataFrame from a dictionary?
- What is the purpose of the
columnsparameter? - What information does
df.shapereturn? - What is the difference between
df["Name"]anddf[["Name"]]? - How can you inspect the data types of DataFrame columns?
- How can you create a calculated column?
Summary
A Pandas DataFrame is a two-dimensional labelled data structure that organizes information into rows and columns. You learned to create DataFrames from dictionaries, lists of dictionaries, and lists of lists.
You also explored DataFrame dimensions, column labels, data types, and row previews. We used single and multiple column selection and created a calculated column for student marks analysis. DataFrames are fundamental to data analysis because they allow you to organize, inspect, transform, and calculate information in a structured format.