Simple Python Program for Data Analysis: Easy Code Examples

Python, with its simplicity and versatility, has become a go-to language for data analysis. Its rich ecosystem offers libraries like NumPy, Pandas, Matplotlib, and Scipy, which streamline data manipulation, analysis, and visualization. Let's dive into creating a simple Python program for data analysis using these powerful tools.

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

First, we'll ensure our environment is ready. Install the required libraries using pip:

30-Day Python Learning Plan for Beginners | Complete Python Roadmap to Learn Coding Fast
30-Day Python Learning Plan for Beginners | Complete Python Roadmap to Learn Coding Fast

pip install numpy pandas matplotlib scikit-learn

Data Manipulation and Cleaning

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

Data analysis often starts with acquiring and cleaning data. Pandas provides DataFrame, a 2-D labeled data structure with columns of potentially different types. It's excellent for manipulating tabular data.

Let's load and explore a simple CSV dataset:

Python Notes for Beginners (Easy + Quick Guide)
Python Notes for Beginners (Easy + Quick Guide)

Importing and Exploring Data

First, import the necessary libraries:

import pandas as pd
import numpy as np

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

Loading and Exploring Data

Next, load a CSV file into a DataFrame:

data = pd.read_csv('data.csv')

Python Cheat Sheet for Beginners 2026 | Python Basics, Syntax, Loops, Functions & Variables
Python Cheat Sheet for Beginners 2026 | Python Basics, Syntax, Loops, Functions & Variables

Inspect the first five rows with:

print(data.head())

Python Data Structures Cheat Sheet for Beginners (Lists, Tuples, Sets & Dictionaries)
Python Data Structures Cheat Sheet for Beginners (Lists, Tuples, Sets & Dictionaries)
Python Basics Cheat Sheet for Beginners | Learn Coding Step by Step
Python Basics Cheat Sheet for Beginners | Learn Coding Step by Step
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 Cheatsheet for Beginners | Learn Python Fast
Python Cheatsheet for Beginners | Learn Python Fast
Exploratory Data Analysis Using Python
Exploratory Data Analysis Using Python
All Important Python Functions for Beginners (Complete Cheat Sheet)
All Important Python Functions for Beginners (Complete Cheat Sheet)
a poster with different types of functions in python
a poster with different types of functions in python
Python Learning Path for Beginners 2026 🚀
Python Learning Path for Beginners 2026 🚀
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
the top 60 python project ideas for beginners to use in their web development projects
the top 60 python project ideas for beginners to use in their web development projects
a paper with some writing on it
a paper with some writing on it
Python Cheat Sheet for Beginners
Python Cheat Sheet for Beginners
Python data types cheat sheet
Python data types cheat sheet
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
the visualizing data with python
the visualizing data with python
🐍 Python Roadmap for Beginners (2026) | Complete Learning Path
🐍 Python Roadmap for Beginners (2026) | Complete Learning Path
Python array sorting guide #python #programming #tutorial
Python array sorting guide #python #programming #tutorial
Python Learning Path for Beginners 2026 🚀
Python Learning Path for Beginners 2026 🚀
Python Functions Cheat Sheet
Python Functions Cheat Sheet
File Handling in Python Cheat Sheet (Chapter 5/15) | Python for Data Analysts
File Handling in Python Cheat Sheet (Chapter 5/15) | Python for Data Analysts

Check the data types and number of non-null values:

print(data.info())

Data Cleaning and Preprocessing

Real-world data often contains missing values, duplicates, or inconsistent data. Let's handle these issues.

Handling Missing Values

Fill missing numeric data with the mean using:

df['column_name'].fillna(df['column_name'].mean(), inplace=True)

Fill missing categorical data with the mode:

df['column_name'].fillna(df['column_name'].mode()[0], inplace=True)

Removing Duplicates

Remove duplicate rows based on a specific column:

df.drop_duplicates(subset='column_name', inplace=True)

Or drop duplicates based on all columns:

df.drop_duplicates(inplace=True)

Data Analysis and Visualization

Now that our data is clean, we can analyze and visualize it. Matplotlib and Seaborn libraries offer extensive plotting capabilities.

Plotting Data

Let's create a simple bar chart. First, import the necessary libraries:

import matplotlib.pyplot as plt

Then, create a bar chart:

data['column_name'].value_counts().plot(kind='bar')

Add a title and rotate xticks:

plt.title('Column Name Distribution')
plt.xticks(rotation=0)

Finally, display the plot:

plt.show()

Exploratory data analysis involves iterating and trying different visualizations to understand data better.

Python's simplicity and extensive libraries make it an ideal choice for data analysis. Start with small scripts, then scale up as you gain confidence and familiarity.

Whether you're a seasoned analyst or a beginner, unlocking the power of Python for data analysis is a rewarding journey. Happy coding!

Related Articles

Simple Python Game Code For Kids If Else Code Example Python How To Run A Simple Code In Python Xgboost Examples Python Json Save Pretty Gurobi Code Example Python Random Sample From List With Seed Gurobi Python Example Simple Python Coding Questions Sample Json File For Python