Introduction to Pandas DataFrames
Explore the fundamentals of pandas DataFrames by loading and inspecting data, selecting and filtering records, and summarizing key metrics like no-show rates. This lesson helps you work confidently with tabular data in Python, handling CSV and Excel inputs to prepare and analyze real-world datasets.
A clinic operations report needs to answer a concrete question: which clinic has the highest appointment no-show rate this month? The appointment records arrive as comma-separated values (CSV) with four columns: clinic, date, scheduled, and attended. Although NumPy works well for numeric grids, appointment records need column names so we can ask questions such as “show only North clinic records.”
A pandas DataFrame is an in-memory table with named columns and rows. It resembles a SQL query result, but Python can continue transforming the result after loading it. The complete pipeline below creates a small CSV in memory, loads it with pd.read_csv, inspects the first rows, and checks the column data types. It expects one header row and consistent values in each column.
Lines 7–15: The CSV is defined as an in-memory string and wrapped with
io.StringIOsopd.read_csvcan parse it exactly like a file, producing a DataFrame withclinic/dateas text andscheduled/attendedas integers.Lines 17–19:
df.head()shows the first few clinic-day rows, anddf.dtypesverifies the inferred column types before any numeric calculations (like no-show rates) are attempted.
This code produces a table where each row represents one clinic-day appointment record. The clinic and date columns load as text, while scheduled and attended load as integers. Checking df.dtypes matters because subtraction and division need numeric columns, while text filtering uses text methods.
Select, filter, and sort appointment records
Once df holds the appointment table, selection and filtering reduce the table to records relevant to a decision. Selecting df['clinic'] returns a Series, which is one labeled column. Selecting df[['clinic', 'date', 'attended']] returns a DataFrame because the double brackets request a list of columns.
A boolean condition produces ...