> ## Documentation Index
> Fetch the complete documentation index at: https://docs.encodebox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Cohort

> Cohort entry, include/exclude criteria, attrition, and specs.

A **Cohort** defines a study population: entry EventFrame → include / exclude criteria → attrition → DataFrame.

```python theme={null}
cohort = (
    eb.Cohort("t2dm", database=db)
    .entry(index, index="first")
    .include(db.demographic.age_at_index(18, 89))
    .include(db.enrollment.covering_index(days_before=365, days_after=0))
    .include(other_frame, window=(-365, 0))
    .exclude(db.diagnosis.matching("C", match="starts_with"), window=(-365, 0))
)

print(cohort.attrition())
final = cohort.to_df()
```

## Building blocks

| Method                          | Meaning                                 |
| ------------------------------- | --------------------------------------- |
| `.entry(frame, index="first")`  | Starting population + index date (once) |
| `.include(source, window=…)`    | Require criterion / EventFrame          |
| `.exclude(source, window=…)`    | Remove patients who meet criterion      |
| `.attrition()`                  | Remaining / dropped counts per step     |
| `.spec()` / `eb.from_spec(...)` | Serialize / restore                     |
| `.to_df()` / `.to_sql()`        | Run / inspect                           |

## Criteria helpers

| Helper                                                  | Meaning                            |
| ------------------------------------------------------- | ---------------------------------- |
| `db.demographic.age_at_index(lo, hi)`                   | Age at index                       |
| `db.enrollment.covering_index(days_before, days_after)` | Continuous enrollment around index |
| `db.demographic.sex(...)` / `.race(...)`                | Demographics                       |
| `db.death.recorded(window=…)`                           | Death recorded                     |
| `include(frame, window=…)`                              | Any EventFrame as a patient set    |

`window=(before, after)` is days relative to the cohort entry index date.

## Related

* [EventFrame](/api/event-frame)
* [Which API?](/which-api)
* [Migration](/migration)
