Pandas is a library for working with tables in Python. Do not rush through the output. Read the column names, read the data types, and ask what each command is showing you.
In this tutorial, you will:
- import pandas
- load a JSON dataset as a DataFrame
- inspect rows, columns, data types, and summary statistics
- select columns and filter rows
- create simple new columns
- sort data and count categories
- use small pandas methods to answer dataset questions
This part reuses the same exercise style as the previous analytic tools material: load a dataset, inspect it, and answer questions with code.
Before starting:
- Open the
session7folder in Visual Studio Code. - Create and activate your virtual environment.
- Install the requirements:
pip install -r requirements.txt- Create your exercise file:
session7/solutions/exercise-07-01.pypandas: a Python library for working with tabular data.DataFrame: a table with rows and columns.Series: one column from a DataFrame.pd.read_json(...): loads a JSON file into a DataFrame.pd.read_csv(...): loads a CSV file into a DataFrame.head(): shows the first rows.tail(): shows the last rows.dtypes: shows the data type of each column.describe(): gives summary statistics for numeric columns.shape: shows the number of rows and columns.
The dataset is already in:
session7/datasets/Movies.jsonFile: session7/solutions/exercise-07-01.py
import pandas as pd
movies = pd.read_json("datasets/Movies.json")
print(movies)Tip
If Python says the file cannot be found, check that you are running the script from the session7 folder.
Task: Print only the first 10 rows after loading the dataset.
Show solution
import pandas as pd
movies = pd.read_json("datasets/Movies.json")
print(movies.head(10))It is usually better to inspect a few rows instead of printing the whole dataset.
File: session7/solutions/exercise-07-01.py
import pandas as pd
movies = pd.read_json("datasets/Movies.json")
print(movies.head())
print(movies.tail(3))Expected idea:
The first output shows the first 5 rows.
The second output shows the last 3 rows.Task: Print the first 8 rows and the last 8 rows.
Show solution
import pandas as pd
movies = pd.read_json("datasets/Movies.json")
print(movies.head(8))
print(movies.tail(8))The schema tells us what columns exist and what type of data pandas thinks each column contains.
File: session7/solutions/exercise-07-01.py
import pandas as pd
movies = pd.read_json("datasets/Movies.json")
print(movies.shape)
print(movies.columns)
print(movies.dtypes)Note
Questions:
- How many rows and columns are in the dataset?
- Which columns are numeric?
- Which columns contain text?
Task: Print only the number of rows, then print only the number of columns.
Show solution
import pandas as pd
movies = pd.read_json("datasets/Movies.json")
rows = movies.shape[0]
columns = movies.shape[1]
print(rows)
print(columns)Use describe() to summarise numeric columns.
File: session7/solutions/exercise-07-01.py
import pandas as pd
movies = pd.read_json("datasets/Movies.json")
print(movies.describe())Note
What are the minimum, maximum, and mean values for IMDB Rating?
Hint
Look for the IMDB Rating column in the describe() output.
Task: Print the mean, minimum, and maximum Production Budget.
Show solution
import pandas as pd
movies = pd.read_json("datasets/Movies.json")
print(movies["Production Budget"].mean())
print(movies["Production Budget"].min())
print(movies["Production Budget"].max())You can select one column:
File: session7/solutions/exercise-07-01.py
import pandas as pd
movies = pd.read_json("datasets/Movies.json")
print(movies["Title"])You can also select several columns:
print(movies[["Title", "Release Date", "IMDB Rating"]])Task: Select and print only Title, Major Genre, and Distributor.
Show solution
import pandas as pd
movies = pd.read_json("datasets/Movies.json")
print(movies[["Title", "Major Genre", "Distributor"]])Use a condition to keep only some rows.
File: session7/solutions/exercise-07-01.py
import pandas as pd
movies = pd.read_json("datasets/Movies.json")
high_rated = movies[movies["IMDB Rating"] >= 8]
print(high_rated[["Title", "IMDB Rating"]])Tip
Filtering is one of the most important pandas skills. Read the condition carefully:
movies["IMDB Rating"] >= 8This creates a True/False result for every row.
Task: Filter and print movies with Running Time min greater than or equal to 150.
Show solution
import pandas as pd
movies = pd.read_json("datasets/Movies.json")
long_movies = movies[movies["Running Time min"] >= 150]
print(long_movies[["Title", "Running Time min"]])Create a simple column that marks high-rated movies.
File: session7/solutions/exercise-07-01.py
import pandas as pd
movies = pd.read_json("datasets/Movies.json")
movies["High Rated"] = movies["IMDB Rating"] >= 8
print(movies[["Title", "IMDB Rating", "High Rated"]].head())Task: Create a column called Low Rated that is True when IMDB Rating is less than 5.
Show solution
import pandas as pd
movies = pd.read_json("datasets/Movies.json")
movies["Low Rated"] = movies["IMDB Rating"] < 5
print(movies[["Title", "IMDB Rating", "Low Rated"]].head())Use value_counts() to count how often values appear in one column.
File: session7/solutions/exercise-07-01.py
import pandas as pd
movies = pd.read_json("datasets/Movies.json")
print(movies["Major Genre"].value_counts())Note
Which genre appears most often?
Task: Count how many movies there are for each Distributor, including missing values.
Show solution
import pandas as pd
movies = pd.read_json("datasets/Movies.json")
print(movies["Distributor"].value_counts(dropna=False))Use sort_values() to sort a DataFrame.
File: session7/solutions/exercise-07-01.py
import pandas as pd
movies = pd.read_json("datasets/Movies.json")
sorted_movies = movies.sort_values("IMDB Rating", ascending=False)
print(sorted_movies[["Title", "IMDB Rating"]].head(10))Task: Sort movies by Production Budget from highest to lowest and print the first 10 rows.
Show solution
import pandas as pd
movies = pd.read_json("datasets/Movies.json")
sorted_budget = movies.sort_values("Production Budget", ascending=False)
print(sorted_budget[["Title", "Production Budget"]].head(10))For a numeric column, nlargest() is a quick way to find top values.
File: session7/solutions/exercise-07-01.py
import pandas as pd
movies = pd.read_json("datasets/Movies.json")
big_budget = movies.nlargest(5, "Production Budget")
print(big_budget[["Title", "Production Budget"]])Task: Use nlargest() to print the 5 movies with the highest Worldwide Gross.
Show solution
import pandas as pd
movies = pd.read_json("datasets/Movies.json")
top_worldwide = movies.nlargest(5, "Worldwide Gross")
print(top_worldwide[["Title", "Worldwide Gross"]])Use & when both conditions must be true. Put each condition inside parentheses.
File: session7/solutions/exercise-07-01.py
import pandas as pd
movies = pd.read_json("datasets/Movies.json")
popular_action = movies[
(movies["Major Genre"] == "Action")
& (movies["IMDB Rating"] >= 7)
]
print(popular_action[["Title", "Major Genre", "IMDB Rating"]])Task: Filter and print Comedy movies with IMDB Rating greater than or equal to 7.
Show solution
import pandas as pd
movies = pd.read_json("datasets/Movies.json")
good_comedies = movies[
(movies["Major Genre"] == "Comedy")
& (movies["IMDB Rating"] >= 7)
]
print(good_comedies[["Title", "Major Genre", "IMDB Rating"]])Create a basic profit column using worldwide gross minus production budget.
File: session7/solutions/exercise-07-01.py
import pandas as pd
movies = pd.read_json("datasets/Movies.json")
movies["Profit Estimate"] = movies["Worldwide Gross"] - movies["Production Budget"]
print(movies[["Title", "Worldwide Gross", "Production Budget", "Profit Estimate"]].head())Important
This is only an estimate. Real movie profit is more complicated than this.
Task: Create a column called Budget in Millions and print it with the movie title.
Show solution
import pandas as pd
movies = pd.read_json("datasets/Movies.json")
movies["Budget in Millions"] = movies["Production Budget"] / 1_000_000
print(movies[["Title", "Production Budget", "Budget in Millions"]].head())Add your answers to:
session7/solutions/exercise-07-01.pyTasks:
- Load
datasets/Movies.jsoninto a DataFrame calledmovies. - Print the first 5 rows, the last 3 rows, and the number of rows and columns.
- Print the data type of each column and summary statistics for numeric columns.
- Print only the
Title,Release Date, andIMDB Ratingcolumns. - Filter and print movies with
IMDB Ratinggreater than or equal to8. - Create a new column called
Long Moviethat isTruewhenRunning Time minis greater than or equal to120. - Count the number of movies per
Major Genre. - Sort by
IMDB Rating, print the top 10 rows, and add a short comment explaining why inspecting the schema is useful before cleaning data.
Complete the following quiz.
quizmd quizzes/python-session-07-part-01-quiz.mdIf you want to choose a theme:
quizmd --theme light quizzes/python-session-07-part-01-quiz.md
quizmd --theme dark quizzes/python-session-07-part-01-quiz.mdYou are now ready to move to the next tutorial.