Unit III · Basics of Python Programming for Pharmaceutical Sciences (BP101T) · As per PCI B.Pharmacy Syllabus, NEP 2020
This topic brings together everything covered so far in Unit III — lists, dictionaries, CSV files, and structured healthcare data — into a single practical skill: loading a small pharmaceutical dataset into Python and performing basic access and manipulation tasks on it. The tool for this job is pandas, the standard Python library for handling structured, table-shaped data.

Why Pandas for Pharmaceutical Data
When you need to work with pharmaceutical data in Python, pandas is the standard tool for handling structured, table-shaped data. It lets you bring external data files — a pharmacy inventory sheet, a clinical trial results spreadsheet — into your program, and then inspect, clean, and transform them for analysis, all with far less code than manually parsing CSV rows one at a time.
Importing Data into a DataFrame
Pandas can load common file formats such as CSV files and Excel spreadsheets directly into a structure called a DataFrame, which behaves like a table with rows and columns — conceptually a spreadsheet you can manipulate entirely in code. Once a file is read in, its contents become available as a DataFrame object that you can query and manipulate using pandas methods.
import pandas as pd
# Load a local CSV file
df = pd.read_csv('pharmacy_inventory.csv')
# Or load an Excel file
# df = pd.read_excel('clinical_trial_results.xlsx')
A single line of code, pd.read_csv(), replaces the multi-step open() / csv.reader() / loop pattern used with the plain csv module — this is precisely why pandas is preferred once a dataset needs more than basic reading.
Basic Data Access
After a dataset is loaded into a DataFrame, pandas provides several simple ways to examine its structure and contents before doing any further processing.
Viewing, Checking, and Summarizing
- Viewing the first rows — calling
df.head()displays the first five rows of the DataFrame, a quick way to confirm the file imported correctly. - Checking the columns — the attribute
df.columnslists the names of every field in the dataset, such asDrug_IDorExpiry_Date. - Getting a summary — calling
df.info()shows the data type stored in each column (integers, strings, dates) and reports whether any values are missing. - Accessing a specific column — indexing the DataFrame with a column name in square brackets and quotes, e.g.
df['Drug_Name'], returns just that column.
import pandas as pd
df = pd.read_csv('pharmacy_inventory.csv')
print(df.head()) # first 5 rows - sanity check the import
print(df.columns) # all column/field names
df.info() # data types and missing-value summary
print(df['Drug_Name']) # a single column, as a pandas Series
head() previews rows, columns lists field names, and info() reports data types plus missing values. Mixing these up (e.g. saying info() shows the first rows) is a common mistake in written answers.Manipulation Tasks
Beyond simply viewing the data, pandas lets you reshape, filter, and compute new information from a dataset to extract insights relevant to pharmacy practice.
Filtering
You can narrow a dataset down to only the rows that meet a certain condition. For example, to keep only the drugs belonging to the “Statin” therapeutic class, you filter the DataFrame based on the Therapeutic_Class column:
statins = df[df['Therapeutic_Class'] == 'Statin']
print(statins)
Sorting
Rows can be reordered based on the values in a chosen column. Sorting the dataset by expiry date, for instance, makes it easy to identify which items will expire soonest — a genuinely useful pharmacy inventory task:
expiring_soon = df.sort_values(by="Expiry_Date")
print(expiring_soon.head())
Calculating
New columns can be created by performing arithmetic on existing columns. Multiplying the price of each drug by the quantity in stock, for example, produces a new column representing the total value of that inventory item:
df["Stock_Value"] = df['Price'] * df['Inventory_Count']
print(df[["Drug_Name", "Stock_Value"]])
Summarizing
Pandas also supports aggregate calculations across a whole column, such as finding the average price of every drug listed in the dataset:
avg_price = df['Price'].mean()
print(f"Average drug price: {avg_price:.2f}")
df[condition]), sorting (sort_values()), calculating (creating a new column), and summarizing (.mean(), .sum()) are the four manipulation categories examiners expect you to name and demonstrate with a one-line code example each.A Complete Worked Example
The following script ties the full workflow together on a small, self-contained pharmaceutical inventory dataset, built directly in code so it can be run without an external file.
import pandas as pd
# A small illustrative pharmacy inventory dataset
data = {
"Drug_Name": ["Paracetamol", "Atorvastatin", "Amoxicillin", "Ibuprofen"],
"Therapeutic_Class": ["Analgesic", "Statin", "Antibiotic", "Analgesic"],
"Price": [2.5, 8.0, 5.5, 3.0],
"Inventory_Count": [500, 200, 150, 300],
"Expiry_Date": ["2027-01-15", "2026-11-30", "2026-06-01", "2027-03-20"]
}
df = pd.DataFrame(data)
# Basic access
print(df.head())
print(df.columns)
# Filtering: keep only Statins
statins = df[df["Therapeutic_Class"] == "Statin"]
print(statins)
# Sorting: soonest-expiring items first
expiring_soon = df.sort_values(by="Expiry_Date")
print(expiring_soon[["Drug_Name", "Expiry_Date"]])
# Calculating: total stock value per drug
df["Stock_Value"] = df["Price"] * df["Inventory_Count"]
print(df[["Drug_Name", "Stock_Value"]])
# Summarizing: average price across the whole inventory
print("Average price:", df["Price"].mean())
Notice how each of the four manipulation tasks — filtering, sorting, calculating, and summarizing — reduces to a single readable line of pandas code, in contrast to the multi-line loops that the same tasks would require using plain Python lists and dictionaries.
csv Module vs pandas: Which to Use When
Both approaches can read and process pharmaceutical CSV data, but they suit different situations.
| Aspect | Built-in csv module | pandas DataFrame |
|---|---|---|
| Data type handling | Everything read as text; manual conversion needed | Types often inferred automatically |
| Filtering / sorting | Requires manual loops and conditions | Built-in one-line methods |
| Aggregation (mean, sum) | Must be coded manually | Built-in (.mean(), .sum(), etc.) |
| Best suited for | Very small files, simple line-by-line tasks | Any real analysis, larger or multi-column datasets |
Recommended Practice Datasets
If you don’t already have pharmaceutical data of your own to practice with, two publicly available resources are commonly recommended for coursework:
- Kaggle Pharma Sales Data — a dataset containing real-world daily sales figures broken down by different categories of drugs.
- FDA Data Files — large, structured data files published by the U.S. Food and Drug Administration covering drug applications and drug approvals.
Frequently Asked Questions
What is the difference between a pandas DataFrame and a plain Python list of dictionaries?
Both represent tabular data conceptually, but a DataFrame adds labeled rows and columns, built-in methods for filtering, sorting, and aggregation, automatic handling of missing values, and much faster performance on large datasets because it is built on NumPy underneath. A list of dictionaries requires you to write manual loops for tasks a DataFrame handles in one line.
Do I need to convert data types after reading a CSV with pandas, the way I do with the plain csv module?
Usually not, or at least far less often. pd.read_csv() automatically attempts to infer sensible data types for each column (numbers stay numeric, dates can be parsed), whereas the standard csv module always returns every value as a plain string. It’s still good practice to check with df.info(), since an unexpected value in a numeric column can cause pandas to treat the whole column as text.
Why filter with df[df['column'] == 'value'] instead of a for loop?
This syntax is called boolean indexing: df['column'] == 'value' produces a Series of True/False values, and wrapping the DataFrame in that condition keeps only the rows where it’s True. It is both far more concise and, for any dataset of meaningful size, considerably faster than manually looping through rows and checking a condition one at a time.
Summary
Pandas is the standard Python tool for importing and working with small pharmaceutical datasets, loading CSV or Excel files directly into a DataFrame with a single function call. Basic data access methods — head(), columns, info(), and column indexing — let you verify and understand a dataset before working with it further. From there, the four core manipulation tasks — filtering, sorting, calculating, and summarizing — cover the vast majority of everyday pharmacy data questions, from finding which drugs are expiring soonest to computing the total value of an inventory.
References
- Pharmacy Council of India (PCI), B.Pharm Regulations, NEP 2020 — BP101T Syllabus, Basics of Python Programming for Pharmaceutical Sciences
- pandas development team, “pandas documentation” — official pandas documentation, pandas.pydata.org/docs
- U.S. Food and Drug Administration, “FDA Data Files” — fda.gov
- Python Software Foundation, official Python documentation, docs.python.org
