Python for Data Analysis 3rd Edition

Author: Wes McKinney
File Type: pdf
Size: 21.9 MB
Language: English
Pages: 579

Python for Data Analysis 3rd Edition: Data Wrangling with pandas, NumPy, and Jupyter

Introduction

Modern engineering generates enormous quantities of data. Sensors, laboratory experiments, manufacturing systems, financial models, simulations, websites, and connected devices can produce thousands or even millions of observations every day. The real challenge is not simply collecting this information—it is transforming raw data into reliable engineering knowledge. 📊⚙️

Python for Data Analysis, 3rd Edition provides a practical way to understand this process through Python-based data manipulation, especially with pandas, NumPy, and Jupyter. These tools form a powerful workflow for importing data, cleaning errors, transforming structures, calculating statistics, identifying patterns, and communicating results.

Python for Data Analysis 3rd Edition

Image

Image

Image

A typical engineering data workflow can be represented as:

Raw Data → Cleaning → Transformation → Analysis → Visualization → Engineering Decision

Jupyter makes this process interactive, allowing engineers and students to combine explanatory text, executable Python code, tables, mathematical calculations, and visualizations in one working environment. Examples of Jupyter-based exploratory analysis commonly begin by importing pandas and NumPy, loading a dataset, inspecting its structure, and then investigating statistical relationships.

The goal is not merely to learn Python commands. The deeper objective is to develop a repeatable engineering method for asking questions of data and producing defensible answers. 🔬


Background Theory

Why Data Wrangling Matters

Raw engineering data is rarely ready for immediate analysis.

A measurement file might contain:

  • Missing values
  • Duplicate observations
  • Incorrect units
  • Inconsistent labels
  • Impossible measurements
  • Different date formats
  • Sensor errors
  • Outliers
  • Mixed numerical and text values

For example, a temperature dataset could contain:

TimeTemperatureLocation
08:0021.5Lab A
08:1521.8Lab A
08:30Lab A
08:45220Lab A

The value 220 °C may be physically impossible for the equipment being monitored. If such a value enters a statistical model without investigation, it can distort the mean, standard deviation, regression coefficients, and engineering conclusions.

This is why data analysis follows a logical sequence rather than jumping directly to visualization or machine learning.

The Mathematical Foundation

NumPy provides the numerical foundation for much of the Python scientific-computing ecosystem.

Consider a vector:

The arithmetic mean is:

The sample standard deviation can be expressed as:

These calculations become especially useful when engineers need to understand measurement variation, experimental uncertainty, or production consistency.

NumPy is particularly useful for arrays and numerical operations, while pandas provides higher-level structures such as Series and DataFrame objects.

From Arrays to DataFrames

A pandas DataFrame can be viewed conceptually as a structured table:

Each row may represent an experiment, machine, customer, sensor reading, or engineering event.

Each column represents a variable.

This structure makes operations such as filtering, grouping, merging, sorting, aggregation, and reshaping much easier.


Definition

What Is Python for Data Analysis?

Python for data analysis is the application of Python programming techniques and specialized libraries to collect, clean, transform, explore, visualize, and interpret structured or semi-structured data.

Three important components are:

NumPy → numerical computation

pandas → data manipulation and wrangling

Jupyter → interactive analysis and documentation

Together, they create a flexible environment for engineering data analysis.

What Is Data Wrangling?

Data wrangling is the process of converting raw, inconsistent information into a structured form suitable for analysis.

A simplified representation is:

The quality of the final result depends strongly on the quality of the transformation process.


Step-by-Step Data Analysis Workflow

Step 1: Import the Required Libraries

A basic workflow commonly begins with:

import numpy as np
import pandas as pd

Additional libraries can be introduced when visualization or statistical modelling is required.

import matplotlib.pyplot as plt

Keeping imports organized makes a notebook easier to understand and reproduce.

Step 2: Load the Dataset

For a CSV file:

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

Then inspect the first observations:

df.head()

The objective is not yet to make conclusions. Instead, ask:

  • What variables exist?
  • What are the units?
  • How many observations are present?
  • Are values missing?
  • Are columns numerical or categorical?

Image

Image

Image

Image

Image

Step 3: Inspect the Dataset

Useful commands include:

df.shape
df.columns
df.info()
df.describe()

For example, df.shape tells you the number of rows and columns.

If the result is:

(5000, 8)

the dataset contains 5,000 observations and eight variables.

Step 4: Identify Missing Values

Missing data can be examined with:

df.isna().sum()

Suppose the output indicates that a pressure column has 150 missing measurements.

You then need to determine why the values are missing before deciding how to handle them.

Possible strategies include:

  • Removing observations
  • Replacing values
  • Forward filling
  • Interpolation
  • Obtaining the original measurement
  • Marking the data as unavailable

Blindly replacing every missing value with zero is generally dangerous because missing ≠ zero.

Step 5: Clean Data Types

Engineering datasets frequently contain numerical values stored as text.

For example:

"25.7"
"26.1"
"27.4"

These values may need conversion:

df["temperature"] = pd.to_numeric(
    df["temperature"],
    errors="coerce"
)

The errors="coerce" option converts invalid values into missing values, which can then be investigated.

Step 6: Filter Relevant Observations

Suppose an engineer wants measurements above 80 MPa:

high_pressure = df[df["pressure"] > 80]

Multiple conditions can also be combined:

result = df[
    (df["pressure"] > 80) &
    (df["temperature"] < 100)
]

This approach allows analysts to isolate operating conditions.

Step 7: Group and Aggregate

Grouping is one of pandas’ most useful capabilities.

df.groupby("machine")["temperature"].mean()

This can answer questions such as:

Which machine has the highest average operating temperature?

Another example:

df.groupby("machine")["energy"].agg(
    ["mean", "min", "max", "std"]
)

Now the engineer can compare operating behavior across machines.

Step 8: Merge Multiple Data Sources

Engineering projects often involve multiple files.

For example:

combined = pd.merge(
    sensor_data,
    maintenance_data,
    on="machine_id",
    how="left"
)

This can combine sensor measurements with maintenance records.

The result can reveal relationships between maintenance events and machine performance.

Step 9: Visualize the Results

A histogram can show the distribution of measurements:

df["temperature"].hist(bins=30)

plt.xlabel("Temperature")
plt.ylabel("Frequency")
plt.title("Temperature Distribution")
plt.show()

A scatter plot can investigate relationships:

df.plot.scatter(
    x="pressure",
    y="temperature"
)

Visualization is not decoration. 📈 It is a diagnostic engineering instrument.


Comparison

pandas vs NumPy vs Jupyter

FeatureNumPypandasJupyter
Primary purposeNumerical computingData manipulationInteractive analysis
Main structureArraysSeries/DataFramesNotebooks
TablesLimitedExcellentDisplays results
StatisticsStrongStrongExecutes analysis
Data cleaningBasicExcellentSupports workflow
VisualizationIndirectIntegrates with toolsExcellent interactive environment
Best useNumerical operationsData wranglingExploration and documentation

pandas vs Traditional Spreadsheet Analysis

CharacteristicSpreadsheetPython + pandas
Manual operationsCommonMinimized
ReproducibilityVariableHigh
Large datasetsCan become difficultMore scalable
AutomationLimited/possibleExcellent
Version controlDifficultStrong
Complex transformationsCan become cumbersomeHighly flexible
Programming knowledgeLowRequired

The important advantage of Python is not that spreadsheets are obsolete. Rather, Python allows repetitive analysis to become programmable, testable, and reproducible.


Diagrams and Tables

The Engineering Data Pipeline

       ┌─────────────────┐
       │   Raw Sources   │
       │ CSV / SQL / API │
       └────────┬────────┘
                ↓
       ┌─────────────────┐
       │ Data Inspection │
       │ shape / info()  │
       └────────┬────────┘
                ↓
       ┌─────────────────┐
       │ Data Cleaning   │
       │ Missing / types │
       └────────┬────────┘
                ↓
       ┌─────────────────┐
       │ Transformation  │
       │ merge / group   │
       └────────┬────────┘
                ↓
       ┌─────────────────┐
       │ Exploratory     │
       │ Data Analysis   │
       └────────┬────────┘
                ↓
       ┌─────────────────┐
       │ Visualization   │
       └────────┬────────┘
                ↓
       ┌─────────────────┐
       │ Engineering     │
       │ Decision        │
       └─────────────────┘

Image

Image

Typical Data-Wrangling Operations

Operationpandas TechniqueEngineering Purpose
Select columnsdf[columns]Isolate variables
Filter rowsBoolean indexingIdentify conditions
Missing valuesisna()Quality control
Remove duplicatesdrop_duplicates()Prevent repeated records
Group datagroupby()Compare systems
Combine datasetsmerge()Integrate sources
Reshapepivot_table()Build analytical summaries
Sortsort_values()Rank measurements
Statisticsmean(), std()Quantify performance

Examples

Example 1: Manufacturing Temperature Analysis

Imagine a factory collecting temperature measurements from three machines.

data = {
    "machine": ["A", "A", "B", "B", "C", "C"],
    "temperature": [72, 75, 81, 84, 68, 70]
}

df = pd.DataFrame(data)

Average temperature by machine:

df.groupby("machine")["temperature"].mean()

The engineer can then identify whether one machine consistently operates at a higher temperature.

Example 2: Energy Efficiency

Suppose:

The calculation can be implemented as:

df[“efficiency”] = (
df[“output_energy”] /
df[“input_energy”]
)

The engineer can then rank machines:

df.sort_values(“efficiency”, ascending=False)

This converts raw measurements into an operational performance indicator.

Example 3: Detecting Extreme Measurements

A simple statistical approach can use the z-score:

Large absolute values may indicate unusual observations.

However, an outlier is not automatically an error. A genuine equipment failure can produce an extreme value that is extremely important from an engineering perspective.


Real-World Applications

Manufacturing Engineering

Factories can analyze:

  • Machine temperatures
  • Vibration
  • Pressure
  • Production rates
  • Downtime
  • Energy consumption

pandas can organize measurements by machine, production line, shift, or date.

Civil Engineering

Engineers can process:

  • Concrete test results
  • Structural monitoring data
  • Traffic measurements
  • Soil properties
  • Weather observations
  • Construction schedules

For example, thousands of sensor readings from a bridge can be grouped by sensor location and time.

Mechanical Engineering

Data wrangling supports:

  • Engine testing
  • Thermal analysis
  • Vibration monitoring
  • Fatigue experiments
  • Manufacturing quality control

Electrical Engineering

Python can process:

  • Voltage
  • Current
  • Power
  • Frequency
  • Harmonic measurements
  • Smart-grid data

Engineers can calculate:

and investigate how power characteristics change over time.

Data Science and Research

Researchers can combine experimental data with statistical analysis, visualization, and machine-learning workflows.

Jupyter is especially useful because analysis, explanatory text, code, and outputs can coexist in one document. This notebook-oriented approach is widely used for exploratory and reproducible analysis.


Common Mistakes

Ignoring Data Quality

A sophisticated model cannot compensate for fundamentally incorrect measurements.

Solution: Always inspect data before analysis.

Treating Missing Values as Zero

This can create false averages and incorrect trends.

Solution: Determine the reason for missingness first.

Modifying Raw Data Directly

Overwriting original datasets makes auditing difficult.

Solution: Maintain raw and processed versions separately.

Using Too Many Notebook Cells Without Structure

Large notebooks can become confusing when variables are repeatedly redefined.

Solution: Organize notebooks into logical sections and create reusable functions for repeated operations. Reproducible notebook workflows benefit from explicit stages for loading, cleaning, analysis, and saving results.

Confusing Correlation With Causation

If two variables move together, that does not prove that one causes the other.

Ignoring Units

Mixing °C and °F, kPa and MPa, or mm and m can produce catastrophic analytical errors.

Always document units.


Challenges & Solutions

ChallengePotential ProblemSolution
Huge datasetsMemory limitationsFilter, aggregate, chunk, or use database tools
Missing valuesBiased analysisInvestigate and document treatment
Inconsistent formatsFailed calculationsStandardize types and units
Duplicate dataInflated statisticsDetect and remove duplicates
OutliersDistorted modelsInvestigate engineering meaning
Complex notebooksPoor maintainabilityModularize code
Multiple data sourcesIncorrect joinsValidate keys before merging
ReproducibilityDifferent resultsFix environments and document steps

For very large datasets, pandas may not always be the ideal final processing engine. Engineers may need SQL databases, distributed computing frameworks, or optimized data-processing systems. The important principle is to select the tool according to the scale and structure of the problem.


Case Study

Predictive Maintenance for an Industrial Pump

Consider an industrial facility monitoring a water pump.

Sensors record:

  • Temperature
  • Pressure
  • Vibration
  • Motor current
  • Operating hours

The raw dataset contains 2 million measurements.

The engineering team creates a pandas workflow to:

  1. Load sensor data.
  2. Standardize timestamps.
  3. Remove duplicate records.
  4. Identify missing sensor readings.
  5. Convert units.
  6. Calculate rolling averages.
  7. Group measurements by operating period.
  8. Compare normal and abnormal operating conditions.
  9. Visualize trends.
  10. Export a cleaned dataset for predictive modelling.

A rolling mean can reduce short-term noise:

If vibration gradually increases while temperature and current also rise, the combined trend may indicate developing mechanical problems.

The important insight is that the Python workflow does not magically diagnose the pump. Instead, it creates structured evidence that engineers can interpret.

This distinction is crucial: data analysis supports engineering decisions; it does not eliminate engineering judgment. 🔧📊


Essential Tips

Start With the Question

Do not begin by asking:

“Which pandas function should I use?”

Begin with:

“What engineering question am I trying to answer?”

Then select the appropriate operation.

Inspect Before Transforming

Use:

df.head()
df.info()
df.describe()
df.isna().sum()

These simple commands can reveal many problems early.

Keep Raw Data Untouched

Use a structure such as:

project/
├── data/
│   ├── raw/
│   └── processed/
├── notebooks/
├── scripts/
└── reports/

This makes experiments easier to reproduce.

Document Engineering Assumptions

If you remove measurements below 0 °C, explain why.

If you remove vibration values above a threshold, document the physical reasoning.

A future engineer should be able to understand why the transformation was performed.

Prefer Reproducible Code

Instead of manually editing thousands of rows, write transformations:

df["pressure_mpa"] = df["pressure_kpa"] / 1000

The code can then be rerun when new data arrives.

Use Visualization as a Quality-Control Tool

Before trusting statistical results, plot the data.

A graph may reveal:

  • Sudden jumps
  • Sensor failures
  • Periodic behavior
  • Drifting measurements
  • Seasonal effects
  • Impossible values

Visualization can therefore act as a bridge between numerical analysis and physical engineering intuition.


FAQs

What is the main purpose of pandas in engineering data analysis?

pandas is primarily used to organize, clean, transform, filter, merge, group, and summarize structured data. Its DataFrame structure is particularly useful for experimental and industrial datasets.

Why is NumPy important?

NumPy provides efficient numerical arrays and mathematical operations. It forms an important foundation for numerical computing in Python.

Why use Jupyter for data analysis?

Jupyter allows engineers to execute code interactively while documenting methodology, displaying tables, and visualizing results in the same environment.

Is Python suitable for beginners?

Yes. Python has relatively readable syntax, while pandas and NumPy provide high-level operations that allow beginners to perform meaningful analysis without implementing every mathematical operation from scratch.

Can Python replace Excel?

Not necessarily. Python and Excel serve different purposes. Excel is excellent for interactive spreadsheet work, while Python is particularly powerful for automation, reproducibility, large-scale transformations, and repeatable analytical workflows.

How should missing engineering data be handled?

There is no universal solution. Missing values should first be investigated. Depending on the situation, engineers may remove observations, interpolate values, use statistical imputation, or preserve missingness as a meaningful condition.

Is pandas enough for very large datasets?

Not always. When datasets exceed available memory or require distributed processing, databases, chunk processing, cloud systems, or distributed frameworks may be more appropriate.

Is data wrangling really necessary before machine learning?

Absolutely. Poor-quality input data can produce unreliable models. Cleaning, validation, transformation, and feature preparation are fundamental stages of a trustworthy machine-learning workflow.


Conclusion

Python for Data Analysis, 3rd Edition: Data Wrangling with pandas, NumPy, and Jupyter represents an important approach to modern technical data analysis: combine numerical computing, structured data manipulation, interactive exploration, and engineering reasoning into one reproducible workflow.

The central idea is simple:

NumPy provides numerical power. pandas provides an efficient framework for manipulating structured datasets. Jupyter provides an interactive environment where engineers can combine code, explanations, tables, and visual evidence.

For students, these tools create a practical pathway from basic Python programming toward professional data analysis. For engineers, they provide a way to automate repetitive calculations, investigate complex datasets, detect abnormal behavior, and build evidence-based decisions.

The most important lesson, however, is not a particular Python function. It is the discipline of treating data as an engineering system: inspect it, validate it, transform it carefully, analyze it mathematically, visualize it intelligently, and document every important assumption. ⚙️🐍📊

That workflow is what turns a collection of numbers into useful engineering knowledge.

Unlock exclusive content
Enjoy all premium content by watching a short ad
Preparing ad...
BY ADX360