# How to Filter Exercises by Body Part in Python: 3 Methods Explained

> Learn to filter exercises by body part in Python using list comprehensions or pandas. Explore 3 effective methods with the exercises-dataset.

- Repository: [Hasan Emir Yıldırım/exercises-dataset](https://github.com/hasaneyldrm/exercises-dataset)
- Tags: how-to-guide
- Published: 2026-08-01

---

**To filter exercises by body part in Python, load the JSON data from [`data/exercises.json`](https://github.com/hasaneyldrm/exercises-dataset/blob/main/data/exercises.json) in the hasaneyldrm/exercises-dataset repository and filter the list using the `"body_part"` key with standard list comprehensions or pandas boolean indexing.**

The hasaneyldrm/exercises-dataset repository maintains a static dataset of 1,324 exercise records in JSON format. Each exercise object contains a `body_part` field that classifies the movement by anatomical target, such as "chest" or "upper legs", making it straightforward to filter exercises by body part in Python without external API calls.

## Understanding the Data Schema

Before filtering, verify the dataset structure against the JSON Schema. The primary data file resides at **[`data/exercises.json`](https://github.com/hasaneyldrm/exercises-dataset/blob/main/data/exercises.json)**, while the validation schema is defined in **[`data/exercises.schema.json`](https://github.com/hasaneyldrm/exercises-dataset/blob/main/data/exercises.schema.json)**. According to the schema specification, the `body_part` property (synonymous with `category`) stores the anatomical classification as a string enum.

You must specify **UTF-8 encoding** when reading the file, as exercise instructions contain non-ASCII characters that will raise `UnicodeDecodeError` with default system encodings.

## Method 1: Filter Using Standard Library json

For lightweight scripts without external dependencies, use Python's built-in `json` module with a list comprehension to select matching records.

```python
import json
from pathlib import Path

# Load the full dataset

DATA_PATH = Path("data/exercises.json")
with DATA_PATH.open("r", encoding="utf-8") as f:
    exercises = json.load(f)

print(f"Total exercises loaded: {len(exercises)}")

# Filter by body part using list comprehension

target_body_part = "chest"
chest_exercises = [ex for ex in exercises if ex["body_part"] == target_body_part]

print(f"Chest exercises found: {len(chest_exercises)}")
for ex in chest_exercises[:5]:
    print(f"- {ex['name']} (ID: {ex['id']})")

```

This approach creates a new list containing only dictionaries where the `body_part` value matches your target string.

## Method 2: Filter Using Pandas DataFrames

For statistical analysis or aggregation, convert the JSON array to a pandas DataFrame and apply boolean indexing.

```python
import json
import pandas as pd
from pathlib import Path

# Load JSON into DataFrame

with Path("data/exercises.json").open("r", encoding="utf-8") as f:
    df = pd.DataFrame(json.load(f))

# Display distribution of body parts

print("Exercise count by body part:")
print(df["body_part"].value_counts())

# Filter for specific body part

upper_legs = df[df["body_part"] == "upper legs"]
print(f"\nUpper-legs exercises: {len(upper_legs)}")
print(upper_legs[["id", "name", "equipment"]].head())

```

**Pandas filtering** enables vectorized operations and method chaining for complex analytics on the exercise dataset.

## Method 3: Advanced Multi-Criteria Filtering

Combine `body_part` filters with other attributes like `equipment` using Boolean masks for precise selection.

```python

# Filter for back exercises requiring no equipment

back_bodyweight = df[
    (df["body_part"] == "back") &
    (df["equipment"] == "body weight")
]

print(f"Back exercises with body weight only: {len(back_bodyweight)}")
print(back_bodyweight[["id", "name"]].head())

```

This technique uses the `&` operator to intersect multiple conditions, returning exercises that match both the body part and equipment requirements simultaneously.

## Key Dataset Files

The hasaneyldrm/exercises-dataset repository organizes its data using the following structure:

- **[`data/exercises.json`](https://github.com/hasaneyldrm/exercises-dataset/blob/main/data/exercises.json)** — Primary dataset containing 1,324 exercise objects with `body_part`, `name`, `equipment`, and `instructions` fields.
- **[`data/exercises.schema.json`](https://github.com/hasaneyldrm/exercises-dataset/blob/main/data/exercises.schema.json)** — JSON Schema Draft 2020-12 definition validating the `body_part` property and other record attributes.
- **[`README.md`](https://github.com/hasaneyldrm/exercises-dataset/blob/main/README.md)** — Human-readable documentation including the complete data schema table and usage examples.

## Summary

To filter exercises by body part in Python from the hasaneyldrm/exercises-dataset:

- Load [`data/exercises.json`](https://github.com/hasaneyldrm/exercises-dataset/blob/main/data/exercises.json) with UTF-8 encoding to handle special characters in instructions.
- Use **list comprehensions** for simple filtering in vanilla Python: `[ex for ex in exercises if ex["body_part"] == "target"]`.
- Use **pandas boolean indexing** for analytical workflows: `df[df["body_part"] == "target"]`.
- Combine multiple criteria using the `&` operator in pandas or chained conditions in standard Python.

## Frequently Asked Questions

### What is the exact field name for body part in the exercises dataset?

The field is named **`body_part`** and is synonymous with the `category` field mentioned in the schema documentation. You can verify this in [`data/exercises.schema.json`](https://github.com/hasaneyldrm/exercises-dataset/blob/main/data/exercises.schema.json) where the property is defined as a string type enumerating anatomical targets like "chest", "back", and "upper legs".

### Can I filter exercises by multiple body parts simultaneously?

Yes. In standard Python, use `ex["body_part"] in ["chest", "shoulders"]` within your list comprehension. In pandas, use the `isin()` method: `df[df["body_part"].isin(["chest", "shoulders"])]` to match multiple values efficiently.

### Do I need to install pandas to filter the exercises dataset?

No. While **pandas** provides convenient DataFrame operations for analysis, you can filter exercises using only Python's built-in `json` module and list comprehensions. The dataset is fully self-contained and requires no external dependencies for basic filtering tasks.

### Why does loading exercises.json throw a UnicodeDecodeError?

The dataset contains non-ASCII characters in exercise instructions. You must specify **`encoding="utf-8"`** when opening the file, as shown in the examples above. Omitting this parameter may cause Python to use the system's default encoding, which often fails on special characters.