Python Data Science Libraries — A Practical Guide to pandas, numpy, and matplotlib
> cd .. / HUB_EDITORIALE
Sviluppo di siti web

Python Data Science Libraries — A Practical Guide to pandas, numpy, and matplotlib

[2026-08-07] Author: Ing. Calogero Bono
> share
Zenithby Meteora Web The operating system for your business. Social, clients, bookings and invoices in one platform. Gyms, barbers, professionals. Discover Zenith Free demo · no card

Your ERP exports a CSV with 40,000 rows of sales, and you need to figure out which products actually make money. Opening Excel means waiting ten minutes and then fighting with filters. Or you have a client asking for traffic analysis, and you need to turn raw numbers into a chart that speaks clearly. The Python data science libraries — pandas, numpy, and matplotlib — are the tools we use to solve these problems every day. They are not for academic research; they are for making operational decisions with solid numbers.

At Meteora Web, we come from accounting: financial statements, double-entry bookkeeping, VAT. When we approach a data analysis project, we think in terms of margins and return, not just pretty charts. With these three libraries, you can clean messy data, calculate product margins, forecast demand spikes, and present everything with a chart your client understands at a glance. In this practical guide, we show you how to use them for real, with copy-paste examples that work.

Why are pandas, numpy, and matplotlib the trio you need for data analysis?

Each library has a specific role, like departments in a company. numpy handles numeric arrays and high-performance math operations: it's the calculation engine. pandas works with tabular data (your tables, CSVs, Excel sheets) and lets you filter, aggregate, merge, and transform data in a few lines of code. matplotlib turns results into charts: histograms, line plots, scatter plots, heatmaps. Together, they cover the entire workflow: load, clean, analyze, visualize.

A common mistake is thinking pandas is enough. But without numpy, operations on large datasets become painfully slow. Without matplotlib, your results remain abstract numbers no one reads. The trio works because each piece does its job. We use it to analyze e-commerce client sales data: with a few lines, we calculate which product has the best margin per season, which category to push in advertising, and which to discount to free up warehouse space. A job that would take hours in Excel takes minutes with Python.

How to install the libraries and set up your environment

Before writing code, you need the libraries installed. The easiest way is pip, Python's package manager:

pip install pandas numpy matplotlib

If you work in a virtual environment (and you should), activate it first. For Anaconda users, the command is similar. After installation, verify everything works by importing the libraries:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

print(pd.__version__)
print(np.__version__)

If you see version numbers, you're ready. Nothing else is needed. Now let's get to the real work.

Sponsored Protocol

How to clean a messy dataset with pandas and numpy?

90% of data science work is cleaning data. CSVs from clients always have issues: inconsistent column names, missing values, duplicates, wrong date formats. If you don't clean, every subsequent analysis is unreliable. An e-commerce client had a file with three years of sales: 15,000 rows, but with mixed date formats and some negative prices (import errors). With three lines of pandas, we normalized everything.

Here's a script that handles the most common issues:

import pandas as pd
import numpy as np

# Load the CSV
# df = pd.read_csv('sales.csv')
# Create a sample DataFrame for testing
df = pd.DataFrame({
    'product': ['A', 'B', 'A', 'C', 'B', None],
    'price': [10.5, 20.0, 10.0, 15.0, 20.5, 30.0],
    'quantity': [2, 1, 3, None, 2, 1],
    'date': ['2025-01-01', '2025-01-02', '2025-01-01', '2025-01-03', '2025-01-02', '2025-01-04']
})

# 1. Remove duplicates
df = df.drop_duplicates()

# 2. Handle missing values: fill with mean for numeric columns
df['quantity'] = df['quantity'].fillna(df['quantity'].mean())

# 3. Remove rows with critical missing data (e.g., product)
df = df.dropna(subset=['product'])

# 4. Convert date to datetime format
df['date'] = pd.to_datetime(df['date'])

# 5. Filter anomalies: negative or zero price
# df = df[df['price'] > 0]

print(df)

This is the starting point for any analysis. drop_duplicates removes duplicate rows, fillna handles missing values, dropna removes rows without essential info. Date conversion is critical: without it, you can't do time-based analysis.

How to handle missing values without skewing results

The choice of how to fill missing values depends on context. If quantity is missing in a sale, you can use the average of other sales. But if a column is like customer tax ID, filling with the mean makes no sense. In that case, better to drop rows or use a sentinel value. Our advice: always document what you do. If the client asks how you handled missing data, you must be able to explain. An analysis with invented data is worse than no analysis.

How to calculate sales KPIs and margins with pandas and numpy?

Once data is clean, the interesting part begins: extracting useful insights. For an e-commerce, key KPIs are total revenue, average margin per product, sales by category, and trends over time. With pandas, it's done in a few lines.

import pandas as pd
import numpy as np

# Sample DataFrame with sales
df = pd.DataFrame({
    'product': ['A', 'B', 'A', 'C', 'B', 'A'],
    'category': ['Tech', 'Home', 'Tech', 'Home', 'Home', 'Tech'],
    'price': [10.5, 20.0, 10.0, 15.0, 20.5, 11.0],
    'cost': [5.0, 12.0, 4.5, 9.0, 11.0, 5.5],
    'quantity': [2, 1, 3, 1, 2, 4],
    'date': pd.to_datetime(['2025-01-01', '2025-01-02', '2025-01-01', '2025-01-03', '2025-01-02', '2025-01-04'])
})

# Calculate revenue and margin per row
df['revenue'] = df['price'] * df['quantity']
df['margin'] = (df['price'] - df['cost']) * df['quantity']

# Total revenue and margin
print('Total revenue:', df['revenue'].sum())
print('Total margin:', df['margin'].sum())

# Average margin per product
margin_per_product = df.groupby('product')['margin'].mean()
print('\nAverage margin per product:')
print(margin_per_product)

# Sales by category
sales_by_category = df.groupby('category')['quantity'].sum()
print('\nQuantity sold by category:')
print(sales_by_category)

# Daily revenue trend
trend = df.groupby('date')['revenue'].sum()
print('\nDaily trend:')
print(trend)

With groupby, you aggregate data by category, product, or date. With sum and mean, you calculate totals and averages. This is the core of analysis: understanding where you make margin and where you lose it. We use it to decide which products to put on promotion: if a product has high margin but sells little, maybe it needs advertising push. If it has low margin and sells a lot, maybe the supplier cost needs renegotiation.

Sponsored Protocol

How to use numpy for fast vectorized calculations

When data grows, pandas uses numpy under the hood. But sometimes you need to intervene directly with numpy for custom calculations. For example, calculating the percentage margin for each product:

import numpy as np

# Convert columns to numpy arrays
prices = df['price'].to_numpy()
costs = df['cost'].to_numpy()

# Calculate percentage margin vectorially
margin_pct = (prices - costs) / prices * 100
print('Percentage margin per row:')
print(margin_pct)

# Calculate average percentage margin
print('Average percentage margin:', np.mean(margin_pct))

With numpy, operations apply to entire arrays without for loops. It's faster and more readable. np.mean, np.sum, np.std are the building blocks for any descriptive statistics. If you need to calculate the standard deviation of sales to understand seasonality, numpy gives you the answer in one line.

How to visualize data with matplotlib for client presentations?

Numbers alone aren't enough. A well-made chart communicates in one second what a table says in ten minutes. With matplotlib, we create professional charts for reports or presentations. Here's how to visualize sales trends and category distribution.

import matplotlib.pyplot as plt
import pandas as pd

# Use the DataFrame from the previous example
# trend = df.groupby('date')['revenue'].sum()

# Line chart for revenue trend
plt.figure(figsize=(10, 5))
plt.plot(trend.index, trend.values, marker='o', linestyle='-', color='#2E86AB')
plt.title('Daily Revenue Trend')
plt.xlabel('Date')
plt.ylabel('Revenue (€)')
plt.grid(True, linestyle='--', alpha=0.6)
plt.tight_layout()
plt.show()

# Bar chart for sales by category
sales_by_category = df.groupby('category')['quantity'].sum()
plt.figure(figsize=(8, 5))
plt.bar(sales_by_category.index, sales_by_category.values, color=['#F18F01', '#C73E1D'])
plt.title('Quantity Sold by Category')
plt.xlabel('Category')
plt.ylabel('Quantity')
plt.tight_layout()
plt.show()

With plt.plot you create line charts, with plt.bar bar charts. You can save charts as PNG or PDF with plt.savefig('chart.png', dpi=150). We recommend using the client's brand colors in reports: a small detail that makes a big difference in perceived quality.

Sponsored Protocol

How to customize charts for a professional report

A standard chart is fine for internal analysis, but for a client you need care. Add clear titles, axis labels, legends if you have multiple series. You can change the style with plt.style.use('ggplot') or 'seaborn-v0_8' for a more modern look. Here's an example with legend and annotations:

import matplotlib.pyplot as plt
import numpy as np

# Sample data
months = ['Jan', 'Feb', 'Mar', 'Apr']
sales_2025 = [12000, 15000, 13000, 18000]
sales_2026 = [14000, 16000, 15000, 20000]

plt.figure(figsize=(10, 6))
plt.plot(months, sales_2025, marker='o', label='2025')
plt.plot(months, sales_2026, marker='s', label='2026')
plt.title('Sales Comparison 2025 vs 2026')
plt.xlabel('Month')
plt.ylabel('Revenue (€)')
plt.legend()
plt.grid(True, linestyle='--', alpha=0.6)

# Annotate the peak point
plt.annotate('2026 Peak', xy=('Apr', 20000), xytext=('Mar', 19000),
             arrowprops=dict(arrowstyle='->', color='red'))
plt.tight_layout()
plt.show()

Annotations with plt.annotate highlight key points. The client doesn't have to search for the peak; you point it out. This is the value we add as an agency: not just data, but immediate visual interpretation.

How to automate data analysis for recurring decisions?

The real leap is automation. Instead of opening the CSV every month and redoing everything by hand, write a script that does it all and sends the report via email. We do this for clients with automatic monthly reports: the script reads data, calculates KPIs, generates charts, and emails everything. The client receives the PDF and doesn't have to ask for anything.

Here's the structure of an automation script:

import pandas as pd
import matplotlib.pyplot as plt
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders

# Function to analyze data
def analyze_data(file_path):
    df = pd.read_csv(file_path)
    # ... cleaning and calculations ...
    return df

# Function to generate chart
def generate_chart(df, output_path):
    trend = df.groupby('date')['revenue'].sum()
    plt.figure(figsize=(10, 5))
    plt.plot(trend.index, trend.values)
    plt.title('Sales Trend')
    plt.savefig(output_path, dpi=150)

# Function to send email with attachment
def send_email(recipient, attachment):
    # Configure SMTP (example with Gmail)
    # ...
    pass

# Main execution
if __name__ == '__main__':
    df = analyze_data('monthly_sales.csv')
    generate_chart(df, 'monthly_report.png')
    send_email('client@company.com', 'monthly_report.png')
    print('Report sent successfully.')

With a script like this, data analysis becomes a repeatable process. You can schedule it with cron on Linux or Task Scheduler on Windows. The time saved is enormous: instead of two hours a month, you spend ten minutes checking everything works. And the client appreciates the punctuality.

Sponsored Protocol

How to schedule analysis with cron on Linux

If your server runs Linux, add a line to crontab to run the script on the first of every month:

0 8 1 * * cd /path/to/script && /usr/bin/python3 analysis.py

This runs the script at 8:00 AM on the first day of each month. Make sure the Python path is correct with which python3. Scheduling is the final step in turning an analysis into a continuous service.

What common mistakes to avoid when using Python data science libraries?

The first mistake is not cleaning data before analysis. If your dataset has duplicates or missing values, KPIs are wrong and the client loses trust. The second mistake is using for loops instead of numpy vectorized operations: on large datasets, code becomes painfully slow. The third mistake is not documenting code: if you need to modify the script six months later, without comments you waste hours.

Another typical mistake is ignoring data types. If the price column is read as a string, you can't do calculations. Always check with df.dtypes and convert with pd.to_numeric if needed. Finally, don't trust charts without context: a chart without a title and labels says nothing. At Meteora Web, we've seen too many useless reports because context was missing.

How to avoid conversion and data type errors

When loading a CSV, pandas tries its best to guess types, but often gets it wrong. Here's how to force conversion:

import pandas as pd

# Force column conversion
df = pd.read_csv('sales.csv', dtype={'price': 'float64', 'quantity': 'int32'})

# Or convert after loading
df['price'] = pd.to_numeric(df['price'], errors='coerce')
df['date'] = pd.to_datetime(df['date'], errors='coerce')

# Check resulting types
print(df.dtypes)

With errors='coerce', non-convertible values become NaN, which you then handle as we've seen. This prevents a typo in the CSV from crashing the entire analysis. Robustness is everything when working with real data.

Sponsored Protocol

How to integrate these libraries into a client workflow?

Imagine a client selling online who asks you to figure out why sales dropped. The workflow is: extract data from their e-commerce (CSV or API), clean it, analyze sales by month, product, acquisition channel, and generate a report with charts. The Python data science libraries are the engine of this process. We do this daily, and the result is that the client knows exactly where to intervene: maybe a specific category is declining, or an advertising channel isn't performing.

The beauty is that you don't need a data scientist: with these three libraries and a bit of practice, a web developer can do powerful analysis. The learning curve is short, especially if you already know Python. And the benefits are immediate: decisions based on data, not intuition.

How to present results to the client effectively

Don't just deliver code. Prepare a document with charts and key conclusions. Explain what each number means and what you recommend doing. For example: "Product X has an average margin of 30%, but sales dropped 15% in the last quarter. We suggest launching a targeted promotion or revising positioning." This is the value an agency like ours adds: turning data into actions.

For more on using Python in web development, check out our complete guide on Python for developers. And if you want to see how these libraries integrate with web applications, take a look at our article on AI agents and automation.

What to do now

Here are concrete actions to start immediately with the Python data science libraries:

  • Install the libraries in your virtual environment: pip install pandas numpy matplotlib.
  • Take a real dataset (even your own sales file) and apply cleaning: duplicates, missing values, data types.
  • Calculate 3 KPIs: total revenue, average margin, sales by category. Use groupby and sum.
  • Generate a chart of the sales trend and save it as PNG with plt.savefig.
  • Automate the process with a script and schedule it with cron.

Nothing else is needed to start. The difference between a good analysis and a bad one is data cleaning and presentation clarity. With these three libraries, you have everything you need. We use them every day and can help you implement a data analysis system for your company or your clients. If you want, contact us.

> share
Ing. Calogero Bono

> AUTHOR_EXTRACTED

Ing. Calogero Bono

Ingegnere informatico, fondatore di Meteora Web e Zenith OS. System administrator e progettista di piattaforme, app e CMS proprietari, con esperienza in sviluppo full-stack, marketing digitale ed ecosistema Google.
[ Read Full Dossier ]

> METEORA_WEB // DIGITAL AGENCY

We build the digital presence your business deserves.

Websites, social media, online advertising, e-commerce and high-performance hosting, engineered with method by computer engineers in Sciacca, for all of Italy.

> MW_JOURNAL

> READ_ALL()