Analyse Sales Data with Python
Load a real CSV, clean data, and extract business intelligence with pandas.
You will learn to
- Import the pandas library and read a CSV file into a DataFrame
- Inspect DataFrame shape, data types, and missing values
- Filter rows based on complex conditions
- Perform data aggregation using groupby()
- Create new derived columns and export results
Before you start
- Basic Python (try Python: Your First Program first)
Tools needed
- The in-browser Jupyter Sandbox (embedded below)
- The sample dataset (download below)
Your workspace
Practise right here. Work through the steps below in this environment.
The notebook runs entirely in your browser (JupyterLite). The first load downloads the Python runtime, give it a minute on slow connections, then it is cached. Use the upload arrow in the file panel to add datasets.
Step-by-step
Tick each task as you finish it, your progress saves on this device.
1. Get the dataset
Download the Teki sample sales dataset. In the Jupyter Sandbox, open the left sidebar (file browser icon) and click the Upload button (arrow pointing up). Select the CSV file you downloaded to upload it to the virtual environment.
Done when: teki-sales-sample.csv appears in the Jupyter file list2. Import pandas and load data
Pandas is the industry standard data analysis library in Python. In the first cell, import it: `import pandas as pd`. Then, load the CSV into a DataFrame (a 2D table) using `df = pd.read_csv("teki-sales-sample.csv")`. Finally, display the first 5 rows by running `df.head()`.
Done when: A nicely formatted table showing the first five rows appears3. Understand the Data Structure
Before analysing, you must understand your data. Run `df.info()` in a new cell. This tells you the total rows, column names, data types, and if any values are missing (nulls). Then run `df.describe()`, which generates summary statistics (mean, min, max, percentiles) for all numeric columns.
Done when: You have viewed both the data structure info and the statistical summary4. Filter for high-value transactions
Let's find the big sales. You can filter dataframes by providing a condition inside brackets. Try filtering for rows where revenue is greater than $500: `high_value = df[df["revenue"] > 500]`. Display `high_value.head()` to verify.
Done when: You have created a new dataframe containing only transactions over $5005. Calculate Revenue by Region
The `groupby` method is incredibly powerful. Let's find out which region generates the most money. Group the data by the "region" column, select the "revenue" column, and calculate the sum: `region_sales = df.groupby("region")["revenue"].sum()`. Sort it so the biggest is at the top: `region_sales.sort_values(ascending=False)`.
Done when: A Series is printed showing total revenue per region, sorted highest to lowest6. Derive new insights
The dataset has `revenue` and `units_sold`. Let's calculate the Average Price per unit. Create a new column in the dataframe: `df["price_per_unit"] = df["revenue"] / df["units_sold"]`. Run `df.head()` again to see your new column appended to the right.
Done when: A new "price_per_unit" column successfully calculated for every row7. Export your findings
Data analysis often ends by sharing results. Save your new dataframe (which now includes the price_per_unit column) back to a CSV file. Run `df.to_csv("enhanced_sales.csv", index=False)`. You will see the new file appear in the file browser!
Done when: The enhanced_sales.csv file is generated and visible in the file explorer
Finished every step?
Mark the lab complete to record it on this device.
Reflect
If you can answer these in your own words, the lab stuck.
- What is the difference between `df.info()` and `df.describe()`?
- Why did we use `ascending=False` when sorting the grouped data?
- What happens if you forget `index=False` when exporting to CSV?