3. Pandas Series & DataFrames for Clinical & QC Laboratory Records
Pandas is the leading Python data manipulation library. It introduces two fundamental structures: the `Series` (a 1-dimensional labeled array) and the `DataFrame` (a 2-dimensional labeled spreadsheet with heterogenous columns). Unlike NumPy arrays, Pandas DataFrames can hold mixed data types (text batch numbers, dates, float assay percentages, boolean release flags), making them ideal for pharmaceutical QC reports and clinical trials.
Building a DataFrame from production batch assays, calculating statistical limits, and flagging Out-of-Specification (OOS) batches.
import pandas as pd
qc_data = {
"Batch_ID": ["B2601", "B2602", "B2603", "B2604", "B2605", "B2606"],
"Drug_Name": ["Ciprofloxacin"] * 6,
"Assay_Percent": [98.4, 101.2, 88.5, 99.8, 102.1, 94.0], # IP Standard: 90.0% - 110.0%
"Hardness_kg": [6.2, 5.8, 4.1, 6.0, 6.5, 3.8], # Target: >= 4.5 kg
"Disintegration_min": [8.2, 9.1, 14.5, 8.0, 7.9, 18.2] # Target: <= 15 min
}
df = pd.DataFrame(qc_data)
# Boolean filtering for compliance
df["Assay_Pass"] = df["Assay_Percent"].between(90.0, 110.0)
df["Hardness_Pass"] = df["Hardness_kg"] >= 4.5
df["DT_Pass"] = df["Disintegration_min"] <= 15.0
df["Batch_Status"] = df["Assay_Pass"] & df["Hardness_Pass"] & df["DT_Pass"]
print("--- QC Batch Release Summary ---")
print(df[["Batch_ID", "Assay_Percent", "Hardness_kg", "Disintegration_min", "Batch_Status"]])| Method / Attribute | NumPy ndarray | Pandas DataFrame | Pharma Use Case |
|---|---|---|---|
| Data Homogeneity | Strictly homogeneous | Heterogeneous (mixed types) | Storing batch codes (str) + assays (float) |
| Labeling | Integer indices only | Custom column names & row index | Column headers for Assay, Hardness, DT |
| File Import | `np.loadtxt()` (numeric only) | `pd.read_csv()`, `pd.read_excel()` | Importing HPLC and dissolution data sheets |
| Missing Data | Limited NaN support | Built-in `.dropna()`, `.fillna()` | Handling missing lab readings in stability studies |
- Pandas Series: 1D labeled array capable of holding any data type.
- Pandas DataFrame: 2D table structure with labeled axes (`index` for rows, `columns` for column names).
- Boolean Indexing: `df[df['Assay_Percent'] < 90.0]` isolates failing batches instantly.
- `df.describe()` outputs summary statistics: count, mean, std, min, 25%, 50%, 75%, max.
Pandas DataFrame एक्सेल शीट की तरह काम करता है। इसमें बैच नंबर, दवा का नाम, प्रतिशत शुद्धता और हार्डनेस एक साथ स्टोर करके फेल बैचों को आसानी से छांटा जा सकता है।