Module 3 · Python for Data Analytics
"DataFrames: load and explore"
Load CSV files into pandas DataFrames, check their size, columns and types, select columns, and get a first feel for the data with describe, value_counts and nunique.
About 30 minutes
The problem
The sales director has sent Bisi three files from Kolanut's system: orders.csv, customers.csv and products.csv, and asked "what's in here?" before anyone builds a report on them.
That question comes first on every analysis. How many rows? Which columns, and what type is each? Are there gaps? What are typical values, and are there any odd ones? Ten minutes of exploring now saves you from a wrong answer later. In pandas, each of those questions is one short line of code.
The concept
pandas and the DataFrame
pandas is the Python library for tables. Its main object is the DataFrame: rows and named columns, like a sheet in Excel. Each column is a Series, one column of values that all share a type. Everyone imports pandas with the short name pd:
import pandas as pdLoading a CSV
pd.read_csv() reads a file from a web address or from your computer:
base = "https://academy.cloudtechanalytics.com/datasets/sales/"
orders = pd.read_csv(base + "orders.csv")
customers = pd.read_csv(base + "customers.csv")
products = pd.read_csv(base + "products.csv")The first questions, in code
| Question | Code | Gives you |
|---|---|---|
| What does it look like? | orders.head() / orders.tail(3) | The first 5 / last 3 rows |
| How big is it? | orders.shape | (rows, columns) |
| What columns? | orders.columns | The column names |
| What types, any gaps? | orders.info() | Each column's type and non-empty count |
| Typical values? | orders.describe() | Count, mean, min, quartiles and max of number columns |
| How often does each value appear? | orders["discount_pct"].value_counts() | Each value and its count |
| How many different values? | orders["customer_id"].nunique() | One number |
Selecting columns
- One column, as a Series:
orders["quantity"]. - Several columns, as a DataFrame:
orders[["order_date", "quantity"]]. Note the double brackets: the inner pair is a list of names.
Series have their own methods: .sum(), .mean(), .min(), .max(), .median(), .count().
Types in pandas
info() shows types with pandas names: int64 (whole numbers), float64 (decimals), object (usually text), datetime64 (dates), bool. A date column that shows as object is being treated as text: it'll sort, but you can't take the month out of it or do date maths. You'll fix that in lesson 5.
Example
Load the three files and look at the orders:
import pandas as pd
base = "https://academy.cloudtechanalytics.com/datasets/sales/"
orders = pd.read_csv(base + "orders.csv")
customers = pd.read_csv(base + "customers.csv")
products = pd.read_csv(base + "products.csv")
print(orders.shape)
orders.head()(4266, 7)
order_id order_date customer_id product_id quantity unit_price discount_pct
0 10001 2025-01-01 27 3 14 18600 0
1 10002 2025-01-01 56 1 7 13200 0
2 10003 2025-01-01 37 2 4 3600 0
3 10004 2025-01-01 26 7 28 6000 5
4 10005 2025-01-01 27 8 19 10500 5Each row is one order line: one product on one order. The numbers on the left (0, 1, 2…) are the index, pandas' row labels.
orders.info()info() reports 4,266 non-null values in every column, so nothing is missing, and shows order_date as object: text, for now.
orders.describe().round(1)From describe(): quantities run from 1 to 30 cartons with an average of 13.8, unit prices from ₦3,600 to ₦24,600, and discounts are 0, 5 or 10%.
Walkthrough
- Open a new Colab notebook and run the Example's first cell to load the three files.
- Run
customers.head()andproducts(a table this small you can just display). Note how they link to orders:customer_idandproduct_id. - Check the sizes:
customers.shapeis (90, 8) andproducts.shapeis (16, 4). - Run
orders["discount_pct"].value_counts(): 2,519 lines had no discount, 1,222 had 5% and 525 had 10%. - Add
normalize=Trueto get shares instead:orders["discount_pct"].value_counts(normalize=True). About 59% of lines had no discount. - How many different customers ordered?
orders["customer_id"].nunique()gives 90: every customer has at least one order. - Select two columns with double brackets,
orders[["order_date", "quantity"]].head(), and compare it with the single-bracketorders["quantity"].head(). One is a table, the other a single column. - Add a text cell summarising the dataset in three sentences: what one row is, the date range, and anything you'd want to check.
Practice
Practice
Which product_id appears in the most order lines?
Practice
How many customers in customers.csv are in the Wholesale channel?
Practice
What is the median credit_limit of Kolanut's customers, in naira?
Challenge
Challenge · optional
How many different sales reps look after Kolanut's customers?
Check your understanding
Answer every question to check.