Python Code for Data Analysis Example: Real World Data Science Projects

Logan Jul 13, 2026

Python, with its powerful libraries and intuitive syntax, has become an exceptionally popular language for data analysis. Its ability to handle data manipulation, visualization, and statistical modeling makes it an excellent choice for both beginners and experienced data analysts alike.

Python Data Structures Cheat Sheet for Beginners (Lists, Tuples, Sets & Dictionaries)
Python Data Structures Cheat Sheet for Beginners (Lists, Tuples, Sets & Dictionaries)

Data analysis involves extracting insights and knowledge from structured or unstructured data. Python provides numerous libraries, such as NumPy, Pandas, Matplotlib, and Scikit-learn, that simplify this process. Let's delve into a simple Python code example illustrating data analysis using these libraries.

Python For Data Analysis: A Complete Guide For Beginners
Python For Data Analysis: A Complete Guide For Beginners

Data Exploration and Cleaning with Pandas

Pandas is an open-source data manipulation library that offers data structures and functions for manipulating and analyzing structured data. Let's assume we have a CSV file named 'sales_data.csv', and we want to explore and clean the data.

7 Python Libraries Every Data Analyst Must Learn in 2026
7 Python Libraries Every Data Analyst Must Learn in 2026

First, we import the necessary libraries and load the dataset:

```python import pandas as pd # Load dataset sales_data = pd.read_csv('sales_data.csv') ```

Exploring the Data

Ultimate Python Cheat Sheet for Beginner
Ultimate Python Cheat Sheet for Beginner

We can start by exploring the data. Let's check the first few rows and the data types of the columns:

```python print(sales_data.head()) print(sales_data.dtypes) ```

Handling Missing Data

Real-world datasets often contain missing or null values. Pandas represent these as NaN (Not a Number). We can handle these NaN values by either removing the rows containing them or filling them with a suitable value.

a book cover with the words python concept for data analyses on it and an image of a
a book cover with the words python concept for data analyses on it and an image of a

```python # Dropping rows with missing 'Sales' data sales_data = sales_data.dropna(subset=['Sales']) # Filling missing 'Category' data with 'Unknown' sales_data['Category'].fillna('Unknown', inplace=True) ```

Data Visualization with Matplotlib and Seaborn

Matplotlib is a popular data visualization library in Python. Seaborn is a high-level data visualization library built on top of Matplotlib, providing a more concise and aesthetically pleasing interface.

Let's visualize the 'Sales' data. We'll first import Seaborn and Matplotlib, then create a bar plot to compare the total sales by category:

Python data types cheat sheet
Python data types cheat sheet

```python import seaborn as sns import matplotlib.pyplot as plt # Creating a bar plot using Seaborn sns.barplot(x='Category', y='Sales', data=sales_data) plt.title('Total Sales by Category') plt.show() ```

Distributions and Outliers

Seaborn's 'distplot' function can help us visualize the distribution of our data and identify any potential outliers. Let's create a distribution plot for our 'Sales' data:

Python Cheatsheet for Beginners | Learn Python Fast
Python Cheatsheet for Beginners | Learn Python Fast
a poster with different types of functions in python
a poster with different types of functions in python
Python Cheat Sheet for Beginners
Python Cheat Sheet for Beginners
Exploratory Data Analysis with Python Code Examples
Exploratory Data Analysis with Python Code Examples
All Important Python Functions for Beginners (Complete Cheat Sheet)
All Important Python Functions for Beginners (Complete Cheat Sheet)
Python Cheat Sheet for Beginners 2026 | Python Basics, Syntax, Loops, Functions & Variables
Python Cheat Sheet for Beginners 2026 | Python Basics, Syntax, Loops, Functions & Variables
Feature Engineering with Python Cheat Sheet (Chapter 9/15) | Python for Data Analysts
Feature Engineering with Python Cheat Sheet (Chapter 9/15) | Python for Data Analysts
Python Basics Cheat Sheet for Beginners | Learn Coding Step by Step
Python Basics Cheat Sheet for Beginners | Learn Coding Step by Step
Matplotlib Cheat Sheet for Data Analysts (Chapter 6/15) | Python for Data Analysts
Matplotlib Cheat Sheet for Data Analysts (Chapter 6/15) | Python for Data Analysts
Exploratory Data Analysis (EDA) Cheat Sheet (Chapter 8/15) | Python for Data Analysts
Exploratory Data Analysis (EDA) Cheat Sheet (Chapter 8/15) | Python for Data Analysts
Python Notes
Python Notes
Data Visualization in Python using Matplotlib
Data Visualization in Python using Matplotlib
Python Data Visualization Libraries Explained (Matplotlib, Seaborn, Plotly)
Python Data Visualization Libraries Explained (Matplotlib, Seaborn, Plotly)
Exploratory Data Analysis Using Python
Exploratory Data Analysis Using Python
Python Coding Cheat Sheet - Code Conquest
Python Coding Cheat Sheet - Code Conquest
a poster with the words python eda and other symbols in purple, blue, and red
a poster with the words python eda and other symbols in purple, blue, and red
Python Conditional Statements Cheat Sheet
Python Conditional Statements Cheat Sheet
the diagram shows how to use python commands for web pages and other contents, including data
the diagram shows how to use python commands for web pages and other contents, including data
Python Cheat Sheet 🚀 | Beginner to Advanced | Save This for Coding & Interviews
Python Cheat Sheet 🚀 | Beginner to Advanced | Save This for Coding & Interviews
Essential Python Skills for Data Science | Beginner to Advanced Guide
Essential Python Skills for Data Science | Beginner to Advanced Guide

```python # Creating a distribution plot sns.distplot(sales_data['Sales'], kde=False) plt.title('Distribution of Sales') plt.show() ```

Correlation Analysis

Before performing any statistical modeling or making data-driven predictions, it's crucial to understand the relationship between the variables in our dataset. A correlation matrix can help us visualize these relationships:

```python # Creating a correlation matrix corr_matrix = sales_data.corr() sns.heatmap(corr_matrix, annot=True, cmap='coolwarm') plt.title('Correlation Matrix') plt.show() ```

By exploring, cleaning, visualizing, and understanding our data, we've laid a strong foundation for more advanced data analysis and modeling. Python, with its extensive ecosystem of libraries, has made this process not only possible but also accessible and efficient.

Data analysis is a continuous process, and each step provides new insights and opens up new avenues for exploration. So, keep exploring, keep learning, and let your data guide your journey.