Data Science DataFrames
Use labelled rows and columns to keep measurements aligned.
How it works
A DataFrame represents a labelled table. Its index is a label system, not automatically a unique row identifier or a meaningful time axis. Calculations between labelled series align by labels, which is helpful when intentional and dangerous when overlooked. Start by making the row grain and column units explicit, then inspect the shape and index before combining results.
Run the code
Try in playground opens the correct pandas editor and preloads the complete source. Choose Run to execute; loading does not execute it. The isolated browser runtime does not read personal PC files.
Local review: Python 3.12.14, NumPy 2.5.4, Pandas 3.0.6 and SciPy 1.18.0. Browser review: Pyodide 0.27.7, Python 3.12.7, NumPy 2.0.2, Pandas 2.2.3 and SciPy 1.14.1 in their respective editors. Save the download as dataframes.py and run python dataframes.py in your separate environment. Apache and MySQL are not needed. Environment setup.
Example
import pandas as pd
daily = pd.DataFrame({"litres": [18., 22.], "hours": [3., 4.]}, index=["Mon", "Tue"])
daily["rate"] = daily["litres"] / daily["hours"]
assert daily.index.is_unique
assert daily.loc["Mon", "rate"] == 6.
assert daily.loc["Tue", "rate"] == 5.5
print("Mon 6.0 L/hour; Tue 5.5 L/hour")
Expected result
Mon 6.0 L/hour; Tue 5.5 L/hour
The numerator and denominator use the same labels, so each rate refers to its own day. loc selects a row by name. Reordering the rows would preserve that relationship; converting one side to an unrelated positional array might not. Keep a clear distinction between an index used for display and a key used to join facts.
Technical references: NumPy, Pandas and SciPy. For evaluation design see the scikit-learn guide to leakage; these examples do not require scikit-learn.