Python Data Analysis Tutorial: 7 Steps for Beginners
You have heard that Python is the tool for data analysis, but every tutorial either drowns you in theory or jumps straight to code with no explanation. This one is different. It is a complete Python data analysis tutorial that walks you through a real task from start to finish, in 7 plain steps, using one dataset the whole way.
Our example is a week of sales at a phone accessories shop in Computer Village, Ikeja. By the end you will have loaded the data, cleaned a problem in it, created a new column, found which product line earns the most, and drawn a chart. Everything a real analysis needs, nothing it does not.
All you need is Python and pandas. If you have not set up a place to run code yet, our guide to learning Python in Jupyter Notebook gets you ready in minutes.
1. Set up your one tool for this Python data analysis tutorial
Data analysis in Python runs on a single library called pandas. It gives you tables, called DataFrames, and hundreds of ready made tools to work on them. That is why every Python for data analytics tutorial starts here. Import it once at the top of your notebook.
# pandas is the core library for data analysis in Python
# Install it once with: pip install pandas
import pandas as pd
print("pandas is ready to use")
The short name pd is a convention every analyst uses, so your code looks familiar to others. For the wider toolkit, see our list of 10 Python libraries every data analyst should know.
2. Load your data (data analysis using Python tutorial)
Here we build the table by hand so every value is visible. In real work you would load a file, which we cover in how to read a CSV file in Python. This is the true first step of any data analysis using Python tutorial.
# One week of sales at a Computer Village phone accessories shop
data = {
"product": ["Phone Charger", "Earpiece", "Power Bank", "Screen Guard",
"Phone Case", "Power Bank", "Earpiece", "Phone Charger"],
"category": ["Charging", "Audio", "Charging", "Protection",
"Protection", "Charging", "Audio", "Charging"],
"units_sold": [12, 20, 5, 30, 18, None, 15, 9],
"unit_price_naira": [2500, 1500, 9000, 800, 1200, 9000, 1500, 2500]
}
# Build the DataFrame
df = pd.DataFrame(data)
df
Notice the missing value in the units column. That is not a mistake in this tutorial. Real data almost always has gaps, and part of learning is knowing how to handle them.
3. Inspect the data before you touch it
Never analyse a table you have not looked at. Three quick commands tell you its shape, its columns, and where the problems are. This habit is what separates a careful Python tutorial for data analyst work from guesswork.
# The first few rows
print(df.head())
# How many rows and columns?
print("Shape:", df.shape)
# Where are the missing values?
print(df.isnull().sum())
The output tells you the table has 8 rows and 4 columns, and that the units column has exactly 1 missing value. Now you know precisely what to fix before going further.
4. Clean the data (data analytics with Python tutorial)
A missing number will break your maths later, so deal with it now. Here the Saturday Power Bank count was never recorded. For this tutorial we treat a missing count as zero, which is the simplest choice. This cleaning step is the backbone of any honest data analytics with Python tutorial.
# Fill the missing units with 0, then make the column whole numbers
df["units_sold"] = df["units_sold"].fillna(0)
df["units_sold"] = df["units_sold"].astype(int)
# Confirm there are no missing values left
print("Missing values now:", df["units_sold"].isnull().sum())
The count of missing values is now 0. In a real project you would ask the shop for the true figure instead of assuming zero. For deeper methods, read our guide on how to clean messy data in Python with pandas.
Following along and want the complete path? Our free guide, 5 Real World Data Projects You Can Do With Python and SQL as a Beginner, gives you full datasets to practise these exact steps on after this post.
5. Create a new column your analysis needs
Raw data rarely holds the exact number you want. Often you calculate it. Here we work out revenue for each row by multiplying units by price. Creating columns like this is a core move in every Python data analytics tutorial.
# Revenue for each sale = units sold times the unit price
df["revenue_naira"] = df["units_sold"] * df["unit_price_naira"]
# Look at what we built
print(df[["product", "units_sold", "revenue_naira"]])
You now have a revenue column built from two others. This is the moment raw records turn into something you can actually analyse.
6. Group and summarise to find the answer
The real question is which product line earns the most. You answer it by grouping rows that share a category and adding up their revenue. Grouping is the single most useful skill in a Python tutorial for data analytics.
# Total revenue for each category, highest first
summary = df.groupby("category")["revenue_naira"].sum().sort_values(ascending=False)
print(summary)
# The total takings for the whole week
print("Total revenue:", df["revenue_naira"].sum(), "Naira")
The result is clear. Charging accessories lead with 97,500 Naira, ahead of Audio and Protection, and the shop took 195,600 Naira for the week. That is a real business insight, and you found it in three lines. For more on this stage, see how to filter, group, and summarise data in Python with pandas.
7. Visualise the result
A number is good. A picture is better for sharing. We turn the category summary into a bar chart so anyone can grasp it at a glance. Ending with a visual is what makes a Python data analysis tutorial feel complete.
# Import the plotting library
import matplotlib.pyplot as plt
# Plot the category summary as a bar chart
summary.plot(kind="bar")
plt.title("Weekly Revenue by Category in Naira")
plt.ylabel("Revenue (Naira)")
plt.tight_layout()
plt.show()
A clean bar chart appears, with Charging standing tallest. You have taken raw, messy records and turned them into a clear answer and a chart, which is the whole job of data analysis. To push this further, our guide to 5 steps to perform exploratory data analysis in Python is the natural next read.
Summary of the 7 steps
| Step | What you did | Key pandas tool |
|---|---|---|
| 1 | Set up your tool | import pandas |
| 2 | Loaded the data | DataFrame |
| 3 | Inspected it | head, shape, isnull |
| 4 | Cleaned a missing value | fillna, astype |
| 5 | Built a new column | column maths |
| 6 | Grouped and summarised | groupby, sum |
| 7 | Visualised the answer | matplotlib |
What to do next
Run these seven steps once, then do them again with your own numbers. Swap the phone accessories for the products you actually sell, or for POS sales, transport fares, or market prices. The dataset changes, but this workflow, load, inspect, clean, build, group, and chart, stays the same for every analysis you will ever do.
When you are ready to turn these basics into job ready skill, our Python for Data Analysts course takes this exact workflow and builds it into full analyses on real Nigerian datasets, with lessons that stack step by step until you can do it without looking.
References
For deeper reference on the tools used here, see the official documentation:
Post a Comment