Most questions need two tables. merge joins them on a key column:
stays = encounters.merge(patients, on="patient_id")
Each encounter finds its one patient, so the result keeps one row per encounter. Choose the key with care: merging labs with encounters on patient_id pairs every result with every visit of that patient and multiplies the rows. A lab result belongs to one encounter, so its key is encounter_id.
A cohort is a set of patients picked by a rule, such as any diagnosis code starting with E11 (type 2 diabetes): diagnoses["icd10_code"].str.startswith("E11"). Diagnoses sit on encounters, so merge to get the patient, then count patients with .nunique(), not rows.
For each patient's latest result, sort by time and keep the last row of each patient: .sort_values("collected_at").groupby("patient_id").tail(1). The highest result, .max(), answers a different question.