Explore a CSV
Find missing values, see which region and category earn the most, and export the result. Start on sample sales data or your own CSV.
Run itWhat you get: revenue by region
| region | orders | units | estimated_revenue |
|---|---|---|---|
| South | 13 | 82 | 2,349.70 |
| North | 19 | 90 | 2,298.29 |
| East | 16 | 75 | 2,171.31 |
| West | 12 | 57 | 1,954.19 |
The notebook
# Explore a CSV Go from a raw CSV to a ranked, charted answer: check data quality, work out revenue by region and category, and export the result. It opens on a sample `data.csv` of 60 orders. To use your own file, drop a CSV (up to 4 MB) onto the file list on the left, or use **+** then **Import CSV**, and the next cell switches to it; re-run from there. Your file stays in this browser tab; it is sent to ByteRun only if you sign up and carry this project over.
import pandas as pd PATH = "data.csv" # set automatically when you upload a CSV df = pd.read_csv(PATH) if df.empty: raise ValueError(f"{PATH} has no data rows") print(f"Loaded {len(df):,} rows and {len(df.columns)} columns.") def download_link(data, filename, mime): # A link that saves `data` as a file, so a result can leave the notebook without any server. from base64 import b64encode from IPython.display import HTML return HTML(f'<a download="{filename}" href="data:{mime};base64,{b64encode(data).decode()}">Download {filename}</a>') df.head()## Check data quality Look before you clean: nothing is dropped or changed here.
quality = pd.DataFrame({ "dtype": df.dtypes.astype(str), "missing": df.isna().sum(), "missing_%": (df.isna().mean() * 100).round(1), "unique": df.nunique(), }).sort_values("missing_%", ascending=False) print(f"Duplicate rows: {df.duplicated().sum()}") quality## Summarize the columns Numbers and categories read differently, so they get separate summaries.
df.describe().Tfor col in df.select_dtypes(exclude="number").columns: print(df[col].value_counts().to_string(), "\n")## Answer a question: which region earns the most? `estimated_revenue` is `units ร unit_price` per row. It is an estimate: the sample has no discounts, returns or order status.
df["estimated_revenue"] = df["units"] * df["unit_price"] by_region = ( df.groupby("region", as_index=False) .agg(orders=("order_id", "nunique"), units=("units", "sum"), estimated_revenue=("estimated_revenue", "sum")) .sort_values("estimated_revenue", ascending=False) .round(2) ) by_regionimport io import matplotlib.pyplot as plt by_category = df.groupby("category")["estimated_revenue"].sum().sort_values(ascending=False) fig, axes = plt.subplots(1, 2, figsize=(11, 4)) by_region.plot.bar(x="region", y="estimated_revenue", legend=False, ax=axes[0], title="Estimated revenue by region") by_category.plot.bar(ax=axes[1], title="Estimated revenue by category") for ax in axes: ax.set_ylabel("Estimated revenue") plt.tight_layout() plt.show() top = by_region.iloc[0] print(f"{top.region} leads with {top.estimated_revenue:,.2f} ({top.estimated_revenue / by_region.estimated_revenue.sum():.0%} of the total).") buf = io.BytesIO() fig.savefig(buf, format="png", dpi=150) download_link(buf.getvalue(), "revenue.png", "image/png")by_region.to_csv("revenue_by_region.csv", index=False) print("Saved revenue_by_region.csv to the project files.") download_link(by_region.to_csv(index=False).encode(), "revenue_by_region.csv", "text/csv")## Your turn 1. Which category earns the most in the top region? 2. Which single order has the highest `estimated_revenue`? 3. Swap `PATH` for your own CSV and re-run the quality check.
Runtime requirements
- Python
- Python 3.12 (in-browser) or the Python data science environment
- Packages
- pandas, matplotlib
- Sample data
- data.csv
- Environment
- Python data science ยท CPU
Browser or real compute
In your browser this notebook runs on a small Python with limited memory and no installs. A ByteRun project runs the same notebook on a full machine with your files saved, any package, and more memory when you need it.
Start the 60-day free trial with this template