DataFrames with pandas
Why this matters
Almost every AI project starts with a table: customers, documents, prices, model scores. pandas is the Python library for tables, and the machine learning phase uses it in nearly every lesson. It's also one of the most-used libraries in all of Python, so time spent here pays off far beyond AI: reports, data cleaning, automating a spreadsheet you used to edit by hand.
- Load a CSV into a DataFrame and look at its shape, columns and first rows
- Pick columns, and pick rows with conditions combined by & and |
- Add calculated columns, sort, and find the largest values
- Count categories and summarize groups with value_counts and groupby
A spreadsheet you can program
A DataFrame is pandas' table: rows and named columns, like a sheet in a spreadsheet. Each column is a Series, a list of values with a name, which behaves much like a NumPy array. You can build a DataFrame from a dictionary of columns:
import pandas as pd df = pd.DataFrame({ "name": ["Ridgeline tent", "Trail mug", "Camp stove", "Rain jacket"], "category": ["tents", "camp kitchen", "camp kitchen", "apparel"], "price": [289, 14, 65, 120], "rating": [4.6, 4.8, 4.2, 4.5], }) print(df) print(df.shape) print(list(df.columns))
import pandas as pd is the universal habit, so code you read elsewhere looks the same. The printout shows the columns across the top and an index down the left: the row labels, 0 to 3 here. df.shape is (rows, columns), and df.columns lists the column names.
Unlike a spreadsheet, nothing here is a formula in a cell. You ask the table questions in code, and you can run the same questions on next month's file without clicking anything.