Data Science With Python - Lesson 07 - Data Manipulation With Python - Pandas
Data Science With Python - Lesson 07 - Data Manipulation With Python - Pandas
Outline the process to create series and DataFrame with data inputs
NumPy
Why Pandas
Intrinsic data
alignment
Data Structures
Data operation
handle major
functions
use cases
Pandas
Pandas
Features of Pandas
The various features of Pandas make it an efficient library for Data Scientists.
Powerful data
structure
Fast and
High performance
efficient
merging and joining
data wrangling
of data sets
Pandas
Intelligent and Easy data
automated aggregation and
data alignment transformation
Series is a one-dimensional array-like object containing data and labels (or index).
Data 4 11 21 36
0 1 2 3
Label(index)
Data alignment is intrinsic and will not be broken until changed explicitly by program.
Series
Data Input
• Integer
• ndarray 2 3 8 4
• String
• dict
• Python 0 1 2 3
• scalar
Object
• list Label(index)
• Floating Point
Data Types
Series
How to Create Series?
Basic Method
4 11 21 36
S = pd.Series(data, index = [index])
Series
Creating Series from a List
Import libraries
Data value
Index
Data type
We have not created index for data but notice that data alignment is done automatically.
Creating Series from an ndarray
Pass ndarray as an
argument
countrie
s
Data type
Creating Series from dict
A series can also be created with dict data input for faster operations.
Country
Data type
Creating Series from Scalar
Scalar input
Index
Data
index
Data type
Accessing Elements in Series
Data can be accessed through different functions like loc, iloc by passing data element position or index range.
Vectorizing Operations in Series
DataFrame is a two-dimensional labeled data structure with columns of potentially different types.
Data Input
• Integer
• ndarray 2 3 8 4
• String
• dict 5 8 10 1
• Python
• List
Object 0 1 2 3
• Series
• Floating Point Label(index)
• DataFrame
Data Types
DataFrame
Creating DataFrame from Lists
This example shows you how to create a DataFrame from a series of dicts.
Entire dict
Viewing DataFrame
You can view a DataFrame by referring to the column name or with the describe function.
Creating DataFrame from dict of Series
Creating DataFrame from ndarray
Access: Click on the Practice Labs tab on the left side panel of the LMS. Copy or note the
username and password that is generated. Click on the Launch Lab button. On the page that
appears, enter the username and password in the respective fields, and click Login.
Missing Values
Missing Values
The fillna function fills all the uncommon indices with a number instead of dropping them.
Data operation can be performed through various built-in methods for faster data processing.
Data Operation with Functions
While performing data operation, custom functions can be applied using the applymap method.
Access: Click on the Practice Labs tab on the left side panel of the LMS. Copy or note the
username and password that is generated. Click on the Launch Lab button. On the page that
appears, enter the username and password in the respective fields, and click Login.
Data Standardization
Data Standardization
read_hdf
read_excel to_hdf read_clipboard
to_excel to_clipboard
read_csv read_html
to csv to_html
read_json read_pickle
to_json to_pickle
read_sql read_stata
read_sas
to_sql to_stata
to sas
Activity: Sequence it Right!
The code here is buggy. You have to correct its sequence to debug it. To do that, click any two code snippets,
which you feel are out of place, to swap their places.
Problem Statement:
Analyze the Federal Aviation Authority (FAA) dataset using Pandas to do the following:
1.View
a. Aircraft manufacturer name
b. State name
c. Aircraft model name
d. Text information
e. Flight phase
f. Event description type
g. Fatal flag
2. Clean the dataset and replace the fatal flag NaN with “No”
3. Find the aircraft types and their occurrences in the dataset
4. Remove all the observations where aircraft names are not available
5. Display the observations where fatal flag is “Yes”
Analyze the Federal Aviation Authority (FAA) Dataset using
Pandas
Common instructions:
•If you are new to Python, download the “Anaconda Installation Instructions” document
from the “Resources” tab to view the steps for installing Anaconda and the Jupyter
notebook.
•Download the “Assignment 01” notebook and upload it on the Jupyter notebook to access
it.
•Follow the cues provided to complete the assignment.
Analyzing the Dataset
Problem Statement:
A dataset in CSV format is given for the Fire Department of the New York City. Analyze the
dataset to determine:
1. The total number of fire department facilities in the New York city
2. The number of fire department facilities in each borough
3. The facility names in Manhattan
Analyzing the Dataset
Common instructions:
•If you are new to Python, download the “Anaconda Installation Instructions” document
from the “Resources” tab to view the steps for installing Anaconda and the Jupyter
notebook.
•Download the “Assignment 02” notebook and upload it on the Jupyter notebook to access
it.
•Follow the cues provided to complete the assignment.
Key Takeaways
Outline the process to create series and DataFrame with data inputs
a. Created automatically
b. Needs to be assigned
a. Created automatically
b. Needs to be assigned
Data alignment is intrinsic in Pandas data structure and happens automatically. One can also assign index to
data elements.
Knowledge
Check
What will the result be in vector addition if label is not found in a series?
2
The result will be marked as NaN (Not a Number) for missing labels.
Knowledge
Check
What is the result of DataFrame[3:9]?
3
This is DataFrame slicing technique with indexing or selection on data elements. When a user passes the
range 3:9, the entire range from 3 to 9 gets sliced and displayed as output.
Knowledge
Check
What does the fillna() method do?
4
fillna is one of the basic methods to fill NaN values in a dataset with a desired value by passing that in
parenthesis.
Knowledge
Check
Which of the following data structures is used to store three-dimensional data?
5
a. Series
b. DataFrame
c. Panel
d. PanelND
Knowledge
Check
Which of the following data structures is used to store three-dimensional data?
5
a. Series
b. DataFrame
c. Panel
d. PanelND
a. iat
b. iloc
c. loc
d. std
Knowledge
Check
Which method is used for label-location indexing by label?
6
a. iat
b. iloc
c. loc
d. std
The loc method is used for label-location indexing by label; iat is strictly integer location and iloc is integer-
location-based indexing by position.
Knowledge
Check
While viewing a dataframe, head() method will _____.
7
The default value is 5 if nothing is passed in head method. So, it will return the first five rows of the
DataFrame.
Thank You