Skip to main content
Python libraries for
Data Science
Course Overview
1
Data Manipulation &
Analysis with
Pandas
2
Data Manipulation
with Numpy
3
Visualizing data with
Matplotlib
4
Advance data
visualization with
Seaborn
5
Machine learning
with Scikit-Learn
6
Project
Data Manipulation &
Analysis with Pandas
Topics Covered in this Section
1. Introduction to Pandas (Overview & installation)
2. Import files using Pandas
3. Inspecting data with Pandas (Head, tail, info, describe etc.)
4. Slicing & dicing data with Pandas
5. Creating new features with Pandas
6. Aggregating data with Pandas
7. Joining datasets with Pandas
Introduction to Pandas
What is Pandas ?
Pandas is the primary package for performing data
analysis tasks in Python.
Pandas derives its name from panel data analysis and
is the fundamental package that provides relational
data structures (think Excel, SQL type) and a host of
capabilities to play with those data structures.
It is the most widely used package in Python for data
analysis tasks, and is very good to work with cross
sectional, time series, and panel data analysis.
Why Pandas?
• It has a tabular data structure that can hold both homogenous and
heterogenous data. Programming is much easier & faster.
• Very good indexing capabilities that makes data alignment and merging easy.
• Good time series functionality. No need to use different data structures for time
series and cross sectional data. Allows for both ordered and unordered time-
series data.
• A host of statistical functions developed around NumPy and pandas that makes
a researcher’s task easy and fast.
• Easily handles data manipulation and cleaning.
• Easy to expand and shorten data sets. Comprehensive merging, joins, and
group by functionality to join multiple data sets.
Installing Pandas
Pandas is a data wrangling library, pull up a computer & let’s start by installing it:
Installation depending on your environment (ie you installed conda, or have pip):
● pip install pandas
● conda install pandas
Python doesn’t load all of the libraries available to it by default. We have to add an
import statement to our code in order to use library functions. To import a library,
we use the syntax import libraryName. If we want to give the library a nickname to
shorten the command, we can add as nickNameHere.
An example of importing the pandas library using common nickname pd is below.
● import pandas as pd #This will import data into your workspace
Importing files using
Pandas
Using Pandas we can import several formats of data like
● CSV
● Excel
● Text
● JSON
● SQL files
● Web pages/ HTML
All these formats are read using Pandas as a Data Frame.
In this section, we will focus on reading CSV file and see how we can manage
Data Frame with various function using Pandas.
# Example
df = pd.read_csv("filename.csv")
Importing Files
Inspecting data with
Pandas
This is initial and important step after loading file, to check whether the file has
loaded correctly and properly.
Following are functions to check:
● Checking the head & tail of the data frame.
● Getting data information on variables types.
● Understanding the characteristics of data.
● Statistical summary on the variables.
● Dot operator or square bracket to check a specific column.
● Locating specific range or array of columns & rows.
Inspecting Data
Slicing & dicing data with
Pandas
In data manipulation, slicing and dicing is a key activity to subset the data as per
the needs of the analysis. For ex:
1. Reducing the data for only a particular Product OR Geography
2. Taking customers who are working only in corporate
3. Analyzing Sales Representatives that are in corporate sales and sale only one
product
Slicing and Dicing Data
Creating new features with
Pandas
During a Data Science project cycle for various purposes it might be needed to
create a new feature to better understand data and imporve modeling.
Most importantly it is used for Modeling with desire for good fit. Feature
engineering, also known as feature creation, is the process of constructing new
features from existing data to train a machine learning model.
Here are some of the feature creation option with Pandas:
1. Creating new features using conditional assignment of values
2. Creating dummy variables
Creating new Features
Aggregating data with
Pandas
Many wonderful results can be achieved on aggregating key values can lead to
statistical significant features and right feature for modeling.
Here are some of aggregate functions
● mean(): Compute mean of groups
● sum(): Compute sum of group values
● size(): Compute group sizes
● count(): Compute count of group
● first(): Compute first of group values
● last(): Compute last of group values
● max(): Compute max of group values
Aggregating Data
Joining with Pandas
Pandas has full-featured, high performance in-memory join operations that are very similar to relational
databases like SQL.
With use of Merge function or join function, we can set or input what kind of join needs to be performed
with indicating of common key.
Type of Joins:
Inner Join Left Join Right Join Full Outer Join
Joining Data