DEV Community

Cover image for Python for Data Science: How I Use Pandas and NumPy to Transform Raw Data into Actionable Insights
PHILIP KAPLONG
PHILIP KAPLONG

Posted on

Python for Data Science: How I Use Pandas and NumPy to Transform Raw Data into Actionable Insights

As I continue developing my skills in data science, two Python libraries have become central to how I approach data analysis: Pandas and NumPy. These libraries solve different problems, but their integration makes Python a powerful environment for data cleaning, transformation, statistical analysis, and exploratory data analysis (EDA).

In my workflow, I use Pandas to manipulate structured datasets and NumPy to perform efficient numerical computations. Understanding how they work, individually and together, is essential for building reliable data pipelines and producing meaningful insights.

1. Pandas: The Foundation of Structured Data Analysis

Pandas is an open-source Python library built on NumPy. It provides high-level data structures and tools for working with structured and labelled data.

Its two primary data structures are:

Series: A one-dimensional labelled array.

DataFrame: A two-dimensional table consisting of rows and columns, where each column can have a different data type.

When I receive a CSV file, my first step is usually to load the data and investigate its structure before attempting any analysis.

import pandas as pd

df = pd.read_csv("sales.csv")

print(df.head())
print(df.shape)
print(df.info())
print(df.describe())
Enter fullscreen mode Exit fullscreen mode

Each command serves a specific purpose. head() previews the records, shape returns the number of rows and columns, info() reveals column data types and non-null counts, while describe() provides descriptive statistics for numerical columns.

I find this initial inspection important because analysis is only as reliable as the data behind it.

Data cleaning and preprocessing

Real-world datasets are rarely perfect. They may contain missing values, duplicate records, inconsistent formats, and incorrect data types. Before calculating metrics, I investigate these issues.

# Identify missing values
print(df.isnull().sum())

# Identify duplicate records
print(df.duplicated().sum())

# Remove duplicate rows
df = df.drop_duplicates()

# Convert a column to numeric values
df["Sales"] = pd.to_numeric(
    df["Sales"], errors="coerce"
)
Enter fullscreen mode Exit fullscreen mode

Using errors="coerce" converts invalid numerical values into missing values instead of raising an error. I can then investigate those records and decide how to handle them based on the business context.

For missing values, I avoid automatically replacing everything with zero. A missing sales value, for example, does not necessarily mean that no sale occurred. Depending on the dataset, I might remove affected records, use a statistically justified imputation method, or preserve the missing values.

Filtering, grouping, and aggregation

After cleaning the dataset, I use Pandas to investigate specific business questions.

Suppose I want to understand which products generate the most revenue.

# Filter high-value sales
high_sales = df[df["Sales"] > 10000]

# Calculate revenue by product
product_revenue = (
    df.groupby("Product")["Sales"]
      .sum()
      .sort_values(ascending=False)
)

print(product_revenue)
Enter fullscreen mode Exit fullscreen mode

The groupby() method allows me to split data into groups, apply calculations, and combine the results. This is particularly useful for analysing revenue by product, customer segment, region, or month.

I can also use agg() to calculate multiple metrics simultaneously, such as total sales, average sales, and transaction counts. These operations form the foundation of many business intelligence reports and analytical dashboards.

2. NumPy: Efficient Numerical Computing

NumPy, short for Numerical Python, provides support for multidimensional arrays, mathematical functions, statistical operations, and linear algebra.

Its central data structure is the ndarray, an array designed for efficient numerical computation.

import numpy as np

sales = np.array([1200, 1500, 1800, 2000, 2500])

print(np.mean(sales))
print(np.median(sales))
print(np.std(sales))
print(np.min(sales))
print(np.max(sales))
Enter fullscreen mode Exit fullscreen mode

These functions calculate the mean, median, standard deviation, minimum, and maximum, respectively.

I use these statistics during exploratory data analysis to understand the distribution of numerical variables, identify unusual observations, and establish a baseline before more advanced modelling.

For instance, the mean represents the arithmetic average, while the median is less sensitive to extreme values. Comparing the two can help me identify potential skewness in a dataset.

Vectorization and computational efficiency

One of NumPy's most useful features is vectorization. Instead of processing every value individually through a Python loop, I can apply operations to an entire array.

prices = np.array([100, 200, 300, 400])

discounted_prices = prices * 0.9

print(discounted_prices)
Enter fullscreen mode Exit fullscreen mode

This applies a 10% discount to every price in a single operation.

NumPy's array operations are generally more efficient than equivalent Python loops for large numerical workloads because much of the computation is implemented in optimized compiled code.

However, the actual performance improvement depends on the operation, data size, and implementation.

Broadcasting

Broadcasting allows NumPy to perform operations on arrays with compatible shapes without requiring me to manually duplicate smaller arrays.

prices = np.array([100, 200, 300])
tax_rate = 0.16

prices_with_tax = prices * (1 + tax_rate)

print(prices_with_tax)
Enter fullscreen mode Exit fullscreen mode

Here, NumPy applies the same tax rate to every element in the array. Broadcasting becomes particularly useful in numerical simulations, statistical calculations, and machine learning workflows.

3. Integrating Pandas and NumPy in a Data Science Workflow

In practical projects, I rarely use these libraries in isolation. Pandas provides labelled, table-oriented operations, while NumPy provides efficient numerical tools.

Consider a sales dataset containing product names, quantities, and unit prices. I can calculate revenue and classify transactions using both libraries.

import pandas as pd
import numpy as np

df = pd.read_csv("sales.csv")

# Calculate transaction revenue
df["Revenue"] = (
    df["Quantity"] * df["UnitPrice"]
)

# Classify revenue
df["Category"] = np.where(
    df["Revenue"] >= 10000,
    "High Value",
    "Standard"
)

# Summarize revenue by category
summary = df.groupby("Category")["Revenue"].agg(
    ["sum", "mean", "count"]
)

print(summary)
Enter fullscreen mode Exit fullscreen mode

In this example, Pandas handles data loading, column creation, and aggregation. NumPy's where() function applies a condition across the revenue values to classify transactions.

The result is a summarized dataset that can support business reporting or further investigation.

Before using this approach in a production project, I would also validate the quantity and price columns, investigate missing values, and confirm that the revenue calculation matches the business definition.

4. Performance, Memory Management, and Best Practices

As datasets grow, writing code that produces the correct result is not enough. I also need to consider computational efficiency, memory usage, and data integrity.

My key practices include:

  • Use appropriate data types: Avoid unnecessarily storing numerical values as strings, and consider smaller numeric types where their ranges and precision are sufficient.
  • Avoid unnecessary loops: Use Pandas operations and NumPy vectorization when they provide clearer and more efficient solutions.
  • Validate assumptions: Check missing values, duplicates, outliers, and invalid records before calculating business metrics.
  • Use method chaining carefully: Clear transformation steps make analytical workflows easier to maintain and debug.
  • Measure performance: Use profiling and benchmarking rather than assuming one implementation is always faster.
  • Preserve data integrity: Avoid overwriting raw data unnecessarily, and document decisions made during preprocessing.

I also recognise that NumPy arrays and Pandas DataFrames are not interchangeable in every situation. NumPy arrays are well suited to homogeneous numerical data, whereas DataFrames preserve column names, indexes, and heterogeneous column types. Choosing the appropriate structure depends on the task.

Conclusion

My approach to data science is built around the idea that meaningful analysis begins long before a chart or machine learning model is created.

Pandas gives me the tools to inspect, clean, transform, and summarize structured data. NumPy allows me to perform numerical computations efficiently and apply mathematical operations across large collections of values.

Together, these libraries provide a strong foundation for exploratory data analysis, statistical investigation, feature engineering, and preparation for machine learning.

As I continue advancing my technical skills, I see mastering Pandas and NumPy not simply as learning Python libraries, but as developing the ability to transform imperfect data into reliable evidence for decision-making.

Ultimately, the value of a data scientist is not measured by the complexity of the code they write, but by the accuracy of their analysis and the quality of the decisions their work supports.

Top comments (0)