Material Coding
Material Coding
(CLO1)
Learning Outcomes
• What is Computational Thinking?
• What are the key components of Computational Thinking?
• An example of applying computational thinking
2
What is Computational Thinking?
• Computational thinking is the thought and processes involved in
formulating a problem and expressing its solution(s) in such a way that
a computer -human or machine- can effectively carry out that solution.
• In other words, it is the ability to formulate a solution that can be
understood by humans and executed by computers.
3
Components of Computational Thinking
• There are four (4) core components of computational thinking:
Decomposition
Algorithm Abstraction
Pattern
Recognition
4
1. Decomposition
• Decomposition: is the process of breaking down a
complex problem or system into smaller and more
manageable parts.
• This could not be achieved without a proper and detailed
understanding of the problem that needs to be solved.
5
2. Abstraction
• Abstraction : The process of identifying important information and
ignoring irrelevant details.
• When you are dealing with a complex problem, there is the
possibility that many components or data will have no relation to
the problem you are trying to solve.
• Identifying and eliminating these components is an important task.
• A key to this process is to look for the information or facts that are
necessary to solve the problem.
6
Abstraction: example
• Find the area of the following shape with the facts provided:
a=5 b=5
width = 6
7
7
Abstraction: exercise
• When you want to know the time, you only look at the hour, minutes
and seconds. Ignore the details like clock shape, colour, make, type
(digital / analog) which are not important.
• A tourist visit Dubai list down the import information he need in dubai
city map?
8
3. Pattern Recognition
• Pattern Recognition: is the process of looking and finding
similarities.
• Patterns may exist amongst the same problem or in different
problems.
• Pattern recognition is important to get a deeper understanding of the
problem and begin formulating a possible solution.
9
Pattern Recognition: example
10
10
4. Algorithm
• Algorithm: is the step by step solution for the given problem.
• The algorithm can be expressed in plain spoken language and/or series of
calculations, or charts such flow charts.
• Algorithm must be
• Have clear and specific instructions.
• Do not miss any steps.
• Give steps in correct order.
11
11
Algorithm: example
• Make a cup of black tea 1. Get an empty cup of tea
2. Determine the type of tea
3. Add the selected tea bag in the cup
4. Determine if sugar is needed
5. Add the amount of sugar needed
6. Add water to a kettle
7. Turn on heat under the kettle
8. Bring water to a boil
9. Add boiling water into the cup
10. Stir the cup with the tea bag
11. Remove tea bag after a minute
12. Serve the tea
13. Done
12
12
Week 2 – Computational Thinking and its Components.
(CLO1)
Learning Outcomes
• What is an algorithm?
• Real life example of an algorithm
2
Lecture 2
Computer Algorithm
3
Learning Outcome
• Apply appropriate sequence and iteration to develop an algorithm
• Develop algorithms to draw various objects
4
Algorithm: reminder
• Algorithm: is the step by step solution to the given problem.
• It is clear and specific, include all steps and has correct order.
• The algorithm can be expressed in plain spoken language and/or
series of calculations, or charts such flow charts.
5
Algorithm
• Sometimes there are different solutions or algorithms to solve the
same problems.
• Some algorithms are faster and more efficient than others.
• There are many ways to solve the same problem.
6
Algorithm: example 1
• List all even numbers from 1 to 1000
• 2, 4, 6, 8, 10, 12....998, 1000
assign 0 to x
for number i = 1 to 500
increase x by 2
Display x
next i
7
Algorithm: example 2
• Find the maximum number in the following list:
• 1, 3, 5, 6, 2, 8, 9, 10, 11, 2, 4
maxNumber = The first number
for every number in the list
if the number is greater than maxNumber
assign number to the maxNumber
next number
print maxNumber
8
Algorithm: example 3
• Find all even numbers in the following list:
• 1, 3, 5, 6, 2, 8, 9, 10, 11, 2, 4
for every number in the list
divide the number by 2
get the remainder of the division
if the remainder = 0
the number is even, print it
else
the number is odd, do nothing
next number
9
Algorithm: exercise
Look around you for real life
problem.
• Use the 4 key components of
the Computational Thinking to Decomposition
produce a solution.
• Write the solution as an
algorithm
Algorithm Abstraction
• Present your solution to your
class mate and seek feedback.
• Summarize the feedback and Pattern
modify your algorithm Recognition
10
10
Algorithm in computer terms
• An algorithm is a step-by-step process producing a solution for a
problem which can be translated into computer code.
• There are four main components of an algorithm: data acquisition,
sequence, selection and iteration.
• Writing an algorithm to solve a simple problem may not require all
these components.
• An algorithm is usually written in plain (simple) human language, and
then translated to a high-level programming language such as Python.
11
11
Algorithm in computer terms
• How to draw a square (100 points on each side)?
• Move pen forward by 100 points
• Change the angle to the right by 90 degrees
• Move pen forward by 100 points
• Change the angle to the right by 90 degrees
• Move pen forward by 100 points
• Change the angle to the right by 90 degrees
• Move pen forward by 100 points
12
12
Algorithm: example 4
• Assume that you are required to
develop an algorithm to draw the shape
on the right.
• How can you apply computational
thinking to this problem?
• Develop the solution/algorithm, and
later use a computer language called
Python to draw this shape.
13
13
Algorithm: example 4 solution
• If you divide the shape into four parts,
examining each part carefully, you will
notice that the shape is made up of
squares.
• Each square is rotated slightly by a
small angle.
• This process of discovery is called
decomposition.
14
14
Algorithm: example 4 solution
• If you draw one square, then change
the angle slightly, draw the next square,
and repeat this multiple times, you will
get the shape.
• Seeing the shape is pattern recognition.
15
15
Algorithm: example 4 solution
• There are other lines in this shape that
are created by having many squares
drawn at different angles.
• If you spend too much time at this stage
trying to understand how they have
been created, you might get confused.
• Focusing on the big picture and finding
the most important part of the problem
is called abstraction.
16
16
Algorithm: example 4 solution
• The next step is writing the solution
step-by-step using simple language.
This is called algorithm.
17
17
Algorithm: example 4 solution
from 1 to the number of boxes
draw a box
change drawing angle by 5
degrees
next box
18
18
What to do next?
• Write algorithms to draw the following objects:
Car Computer
19
Week 3 – Translating Algorithm into a computer code
(CLO1)
Learning Outcomes
• List four of the mostly used high level programming languages
• Recognize four widely used Python packages or libraries
• Convert algorithms from human natural language to computer code
in Python
• Use loop to repeat a block of code
2
High Level Programming Languages
• Computer language also referred to as programming language is a
formal language used to communicate commands to a machine,
typically a computer.
• It is used to create a program to implement a specific algorithm.
3
Example of programming languages
• Java: Used to create programs for mobile phones, web sites, personal
computers, and many other platforms.
• C#, Visual Basic: Used to create programs that run on Microsoft
products.
• Swift: Used to create programs that run on Apple products.
• JavaScript: Used to write games and other programs that run on web
browsers.
• Python: One of the latest computer languages that has unique
capabilities and can run on many devices.
4
How to implement?
• In this course we will be using Python.
• We will use a website called https://repl.it to implement algorithms
5
Why Python Programming Language?
The ability to connect to a wide range of data sources,
integrate with many applications including machine learning,
artificial intelligence, motion graphics, etc.
Package for scientific computing in Python.
t.forward(100)
t.left(90)
7
t.forward(100)
7
8
8
Repetition :
9
The for loop
• The for loop repeats a block of code for number of times.
• In this example, we will repeat 4 times
stop after 4
loops
for i in range(4):
This is the
counter of
repetitions 10
10
The for loop
• Syntax:
11
11
for loop example
: :
Equivalent code
12
12
Some turtle commands
Method or Required
Description
Command Parameters
forward amount Moves the turtle forward by the specified amount
backward amount Moves the turtle backward by the specified amount
right angle Turns the turtle clockwise
left angle Turns the turtle counter clockwise
penup None Picks up the turtle’s pen
pendown None Puts down the turtle’s pen to start drawing
color color name Changes the color of the turtle’s pen
Changes the color of the turtle will use to fill a
fillcolor color name
polygon
position None Returns the current position
goto x,y Move the turtle to position x,y
13 https://trinket.io/docs/colors
Drawing a circle
• Syntax: turtle.circle(radius, extent = None, steps = None)
• radius is the radius of the circle
• extent – an angle – determines which part of the circle is
drawn. If extent is not given, draw the entire circle. If extent
is not a full circle, one endpoint of the arc is the current pen
position.
• steps determines the number of steps to use
• Draw the arc in counterclockwise direction if radius is
positive, otherwise in clockwise direction.
14
14
Drawing a circle
• import turtle as t
• t.circle(50) draw a circle
with a radius of 50 dots.
• t.circle(50, 180) draw a ½
circle with a radius of 50 dots.
• t.circle(50, 180, 4) draw a
½ circle with a radius of 50
dots, 4 steps.
15
15
fillcolor(colorName)
• Fills a closed shape with a color
• Should be used with begin_fill( ) and end_fill( )
• Example:
t.fillcolor("red")
t.begin_fill( )
t.circle(50)
t.end_fill( )
16
16
Exercise
• Draw the following shapes:
17
17
Week 4 – Data Types, Variables, Operators and
Expressions.
(CLO2)
Learning Outcomes
Distinguish between different data types
2
1. Data Types
• In our daily life we use numbers and text to communicate
and perform certain processes.
3
Data Types
long (not
decimal binary octal hex
int available in
26 0b11010 0o32 0x1A
Python 3)
Examples: 26L
Numeric float 10.5
Types 0.105e2
fname age
6
6
2. Rules for naming a Variable
Technical definition of a variable: A variable is a memory location where a programmer
can store a value.
7
Exercise: Variables
Identify valid variable names from the following
1. max_price
2. Total students
3. US$
4. student_id
5. as
6. discount20
7. 1st_student
8. ICT-2013 8
8
Using Variable
• What if you want to change the size of the square from 100 to 50 dots?
• Solution 1:
• Replace all 100 by 50
• Solution 2:
• Use a variable instead of a value!
• How?
9
Using Variable
10
10
3. Arithmetic Operators
Operator Description Example
11
11
Exercise: Operators and Expressions.
Convert the following mathematical expression to Python
expression
1 2
4
3
12
12
4. Precedence Rules
1. ( ) x = 18 / 2 * 3 + 2
2. ** 9 * 3
3. *, /, %, // 27 + 2
4. +, -
29
13
13
Exercise: Precedence Rules.
Evaluate the following expressions using the operator
precedence.
1. 12 + 8 / 4 * 3
2. (12 + 8) / 4 * 3
3. 11 // 3 + 3 ** (4 - 2) * 2
14
14
Summary
Primitive Data types: int, float, string
15
15
Week 5 – Input, Process, Output.
(CLO2)
Learning Outcomes
2
1. Assignment
variable = expression
The = operator assign the expression on the right side to the variable in left side.
a=5 a is 5
x = y = 10 x and y is 10
a, b = 10,5 a is 10, b is 5
s = 10 ** 3 s is 1000
3
amount
Interaction with user
• To read/get a value from the user:
• use the command: input
• Read from the keyboard
4
2. Display statement
print(expression[,expression2, expression3,…])
The print() function prints the given expression on the console screen.
print(5 + 2) 7
5
5
print("5 + 20 = ",5 + 20) 5 + 20 = 25
3. Read user input
variable = input(string)
• Any entry is considered as a string
• Must Convert the entry to the desired data type
Example: ask the user for his name and his age
name = input(“Enter your name:”)
age = input(“Enter your age:”) Returns string
6
Input convertion
x = input(“Enter anything:”)
7
Example 1
Program to read name and age from user and display it.
8
Example 2 – with simple calculation.
Program to read name and age from user and calculate and display his/her age on next year.
9
Example 3
Write a Python program that read price of an item and quantity bought, calculate and display the total.
10
10
Exercise - 1.
11
11
Exercise - 2.
Write a python program that calculate and display
area of the given shape.
height
base
Note: Formula to calculate area of rectangle is
12
12 width
Exercise - 3.
Write a python program that read weight (kg) and
height (m) of a person, calculate and display his BMI
[Body mass index].
Sample output
13
13
13
Exercise - 4.
A basket can hold 5 apples. Write a python program that read total
number of apples, calculate and display the number baskets need and
number of apples that will be without a basket.
(Note: you need to use integer division and modulus operators)
Sample output
14
14
Summary
print() function used to display in standard output [screen]
Input() function used to read input from keyboard and return
as string
= used to assign value / expression on right to a variable in
left.
Program using input process output.
15
15
Week 6 – Selections
(CLO3)
Learning Outcomes
Apply if statement to single or multiple conditions.
2
Selection or Decision
Selections: if statement
• Another example, if you would like to buy an item and you found two similar
products. You will then put some conditions such as a price limit, rating of the
product by experts, etc.
3
Selection or Decision
Selections: if statement (Cont)
• If..else
• nested if
• nested else if
4
Selections: if statement
Structure of if statement in Python:
Rules:
• A lower case if keyword must be used
• The if statement must end with a colon :
• The keyword else must be lower case and start at
the same level as the keyword if
• The keyword else must end with a colon :
5
5
Operator Description Example
Equal == If the values of two operands are equal, then the 2 == 2 [True]
condition becomes True, otherwise False. 3 == 2 [False]
"sum" == "sum” [True]
"ABC" == "Abc” [False]
Python Not Equal != If values of two operands are not equal, then 2 != 2 [False]
Comparison Not Equal <> condition becomes True, otherwise False. 3 != 2 [True]
"sum" != "sum” [False]
Operators "ABC" != "Abc” [True]
Greater than > If the value of left operand is greater than the 3 > 2 [True]
value of right operand, then condition becomes 3 > 3 [False]
True.
Greater than equal If the value of left operand is greater than or 3 >= 2 [True]
>= equal to the value of right operand, then 3 >= 3 [True]
condition becomes True. 3 >= 4 [False]
Less than < If the value of left operand is less than the value 2 < 3 [True]
of right operand, then condition becomes True. 3 < 3 [False]
Less than equal <= If the value of left operand is less than or equal 3 <= 4 [True]
to the value of right operand, then condition 3 <= 3 [True]
becomes True. 6
4 <= 3 [False]
6
Example – 1 (Condition with numeric)
else: Otherwise
Sample output
7
Example – 2 (Condition with non numeric)
Sample output
8
Note: String comparision is case sensitive.
Example - 3
Write a Python
program to calculate
the cost of insurance
based on the age. If
the age of the
subscriber is more
than or equal 40 years,
the cost will be 500
each month, otherwise
the cost is 300 a
month.
9
Exercise – Simple condition
Task 1:
Write a Python program that read mark of a student and display Pass
when given mark is more than or equal to 60, otherwise display Fail.
Task 2:
10
Logical Operators
When you have more than one condition in the same if statement [compound
condition], then you need to use a logical operator. These logical operators simply
allow you to request that both conditions must be met or only one of them.
• If both are conditions must be True then use and.
• If Any one of the conditions is True then use or.
12
Example - 4
13
Task - 1
14
Exercise
15
Week 7 – Nested Selections
(CLO3)
Learning Outcomes
2
3
1. Nested if
3
4
Example - 2
4
5
2. Nested else if
5
6
Example 3
Write a Python
program to ask users
to enter an age. The
program determines
and display a
message as shown in
bellow table.
7
Example 4
8
9
More Example
Grade
Write a Python Grade Range Points
A 90 – 100 4
program to ask users
A- 87 – 89 3.7
to enter a mark out of
B+ 84 – 86 3.3
100. The program
B 80 – 83 3
finds the equivalent
B- 77 – 79 2.7
letter grade using the
C+ 74 – 76 2.3
below grading
C 70 – 73 2
system.
C- 67 - 69 1.7
D+ 64 - 66 1.3
D 60 – 63 1
F 0 – 59 0
9
Example 5
10
10
Exercises
1. if (total = 500)
2. if (rate =>”FIVE” )
3. if (age = 2.5)
4. if (mark =< 40)
5. if (2.85 <= price)
6. if (5+8)
7. if ((1 - 1) <= 0)
8. if (ticketType == "Golden Class")
11
Exercises
Exercise 2: Modify the above example to display the Grade Point Average
(GPA) based on the mark.
Exercise 3: Modify the above program to allow the student to enter the marks
for 5 courses and calculate the semester average and GPA.
12
Exercises
Exercise 5: Write a python program to accept two numbers as input and find
out which is the larger number. If both are equal then print the result as “Both
are Equal”.
13
13
Week 8 – Repetition (for loop)
(CLO3)
Learning Outcomes
Apply for statement to repeat set of statements.
2
The for loop
The for loop repeats a block of code for number of times. In
this example, we will repeat
counter
for i in range(4):
for loop
number of
timers to
repeat
3
Key points to remember
The for loop is one kind of loop. It uses a counter which
takes on a value every time the loop iterates.
4
Version 1: For loop with only end value
for i in range(endValue):
Statements
5
Version 2: For loop with start and end value
Output
Example
6
Version 3: For loop with increment value
for i in range(startValue, endValue, stepValue):
Statements
The starting value of loop can be changed to any given number. Step
value can be change from 1 to any value.
Note: Step value must be negative if start value is greater than end value.
Example Output
7
Example using loop
Exercise 1: Write a program that read 5 marks of a student, calculate and display the
average price.
for i in range(5):
mark = int(input("Enter a mark "))
total_marks = total_marks + mark
avg_mark = total_marks / 5
8
Task
9
Exercises
Exercise 3: Write a program that display all the number from 1 to 25, that is
divisible by 5.
10
10
Week 9 – Introduction to Data Analytics in Python
(CLO4)
Learning Outcomes
Understand the process of Data Analysis
2
Why do I need to learn Data Analytics?
3
The process of Data Analytics
• Well organized and clean data require What are your objectives?
4
1. What are your objectives:
The first step in data analytics is deciding your objectives. What are you trying to
achieve?
5
What is Pandas?
• Pandas is an open source library
providing high-performance, easy-to-
use data structures and data analysis
tools for the Python programming
language.
• It provides fast, flexible, and
expressive data structures designed
to make working with data easy and
intuitive.
• Pandas allows data users to work
with high-level building blocks for
doing practical, real world data
analysis in Python.
6
Importing Pandas to Python
From now and on, you can use the object pd to perform
pandas operations.
7
Data Acquisition: 1. Read data from Excel file.
Pandas allows you to access various sources of data including Excel, Comma
Separated Values (CSV), tables, text, database, web and more.
9
Data Acquisition: 2. Read data from CSV file.
10
Data Acquisition: 3. Read data from Web page (HTML).
11
Pandas Data Frame
DataFrame: DataFrame is a two (or more) dimensional data structure – basically a table with
rows and columns. The columns have names and the rows have indexes.
index
Rows
12
Pandas Data Frame
Let us examine the pandas data frame in more detail.
Index: The index is column zero in the data frame. The default
index is integer number from 0 to the length-1 of the data
frame. Index is used to speed up searching large data.
Later we will learn how to change the index of a data
frame.
Columns: These are the names of the columns as in the original
excel data. My looking carefully at these columns, we will
note that they are not the appropriate columns for the data
set. The correct column names are in the third row. To get
the correct columns we need to skip the first 4 rows. We
will learn later how to rename columns and create new
columns.
Rows: Rows are the actual data excluding columns and index.
You may noticed that there are number of cells with data
NaN. These are the missing or not available data from our
data source. Later we will learn how to deal which such
missing data.
13
Data Cleaning: 1. Skipping unnecessary rows and columns
Pandas allows you to skip unwanted rows when reading the data.
We basically added comma then skiprows=4 to the original line that reads data from
the excel file. Store the result in a new data frame as data2.
14
Without skiprows After skiprows
Data Cleaning: 2. Working with missing data
In many cases you will find your self dealing with missing data. This
is indicated by NaN in the cells or just empty cell. How to deal with
such situation.
1. Drop missing observations 3. Drop column if they only contain missing values
This will delete all rows that contain any missing data. To force dropna to work on columns you need to add axis=1.
2. Drop rows where all cells in that row is NA 4. Fill in missing data with zeros
This will delete rows that are missing all its data. This will replace NaN with 0.
Note:
• keyword how=“all” to indicate all cells have missing data
15 • keyword how=“any” to indicate any cell have missing data
Example: Filling in missing data
In this example we have a simple data set that contains students score pre and post test. The data set
contains number of missing data of students who did not attend the exam. These missing data are
appearing as NaN. Our simple task is to replace NaN with average (mean) score of the column..
The data set is included in the excel file ”Grades.xlsx” that we used in the previous example. It
located in the sheet “PrePost”.
16
Example: Filling in missing data
Fill all the missing values with 0 permanently using the below example.
To fill in the missing data with 0, we will use the following line:
The
function
Brace
rename
type of
bracket
Please note that inplace=True is needed to ensure that the changes
you may are permanent. Otherwise, this line we display the new
column name and if you try to access the same DataFrame, then you
18 will get the old name.
Example: Renaming columns
Following command will rename column Question1, Question2, Question3, and Question4
to Q1, Q2, Q3, Q4.
19
Command summary.
20
Week 10 – Introduction to Data Analytics in Python
(CLO4)
Learning Outcomes
2
Exploring the data: Show information about the data frame.
dataFrame: Display all the rows and columns in the data frame.
3
Example: Show information about the data frame.
1. Display all the rows and columns
4
Slicing the data: Show specific rows / columns of a data frame.
Slicing data is a technique which is used to create small sets of your large data.
dataFrame[start:end+1]: Display all rows start from index start to index end
dataFrame[start:end+1:step]: Display all rows start from index start to index end in the
intervals of step
dataFrame[" Column "]: Display specific column
dataFrame [[" Column1 ", " Column2 ",…]]: : Display specific columns
5
Example: Show specific rows of the data frame.
6
Example: Show specific columns of the data frame.
data
7
Slice data using loc
8
Slice data using loc
The name of Please note Here you specify The colon Here you specify
your data the use of the first column is the last column
frame object. square name. Please separates name. Please
In our example bracket. note that you start and note that you
this is data2. Normal should use column end of the should use column
bracket will name and not columns. It name and not
not work. numbers. is must numbers.
have.
9
Example: Slice data using loc
Note that you need to use the index of the rows and the name of the column.
The column
"Higher Education Institution” :"Enrolled_Post Graduate"
10
Slice data using loc
You can display columns that are not in sequence, you need
to add then inside a square bracket [ ].
Example: Display rows 3, 5, and 5 and Columns "Higher Education Institution” and
"Enrolled _ UnderGraduate"
11
Slice data using iloc
13
Resetting the index
When you need to reset the index back to its original values. There are different ways to
do this. On common method is to run the line that reads the data from your source.
However, you can use the function .reset_index()
14
Statistics / Aggregation Commands
When you need to summaries the data in data frame Pandas makes the calculation of
different statistics very simple.
15 15
Displaying unique values in a column
Finding unique (nonrepeating) values in a column is needed to perform analysis on your
data.
dataFrame["Column"].unique()
For example, to know the unique values in the column "Specialisation" use the function
.unique() that helps you with perform this task.
16
Calculated Columns
Pandas allows you to easily add new columns to the DataFrame. This is usually used to
create a new calculated column.
17
Appending Data : Join two Dataframe
newDataFrame = dataFrame1.append(dataFrame2)
Example: You have two data frames as bellow. Both sheet have the same structure. They contains students
from Education and Foundation Specialisation. We need to combine both in one DataFrame.
dataFrame1 dataFrame2
18
Combained dataFrame
Writing data to external file
Example: Write the data you cleaned in the previous example to an external file.
19 19
Summary of Pandas Commands
Selection Statistics
• df[col] | Returns specific column • df.describe() | Summary statistics for
• df[[col1, col2]] | Returns columns numerical columns
• df[start:end+1,step] Return specific rows • df.mean() | Returns the mean / average
• df.loc[start:end,”col1”:”col2|] Returns specific rows and columns • df.count() | Returns the number of non-
• df.iloc[start:end,col1Index:col2Index] null values
• df.sum() | Returns the sum of values.
Viewing/Inspecting Data • df.max() | Returns the highest value
• df.min() | Returns the lowest value
• df.head(n) | First n rows of the DataFrame
• df.tail(n) | Last n rows of the DataFrame Change index
• df.shape() | Number of rows and columns
• df.unique() | Return unique values • df.set_index(col,inplace=True)
• df.reset_index(inplace=True)
Sort data
Group data
2
Slice data using simple conditions
You can also slice data using conditions based on the value of one or more
columns.
The structure of the command line is: DataFrame[Condition]
Example 1 Display all the rows where "Specialisation" is "Health and Medicine"
3
Slice data using simple conditions
Example 2 Display all the rows where " Enrolled _ UnderGraduate" is 1000 and above
4
Slice data using multiple conditions
To slice data using more than one condition, you need to put each condition inside a bracket ( ).
You also need to use a logical operator. The symbol & is used for and. The symbol | is used for or.
5
Slice data using multiple conditions
Example 2 Display all the rows where "Enrolled _ UnderGraduate" value is from 500 to 800
Condition (data["Enrolled _ UnderGraduate"] >= 500) & (data["Enrolled _ UnderGraduate"] <= 800)
6
Sorting data with one column
Ascending Order
7 Descending Order
Sorting data with more than one column
To sort data based on more than one column, you need to include all
columns inside the square bracket.
Example: Sort Specialisation in ascending order, Enrolled undergraduate in descending order
8
Grouping Data
Grouping data by columns is used when you have duplicated data in a particular column. For example
you, the same student is doing more than one course and has grades in each course. The function
needed for this operation is .groupby.
Syntax
Instead of mean(), you can use sum(), min() or any of the aggregation commands.
9
Grouping Data
Example: Total. Number of students enrolled in each specialisation, by each Institution
10
Summary of Pandas Commands
Filter
• DataFrame[Condition]
• DataFrame[(Condition1) & (Condition2) ]
• DataFrame[(Condition1) | (Condition2) ]
Sort
Group
11
Week 12 – Introduction to Data Analytics in Python
(CLO4)
Learning Outcomes
Data Visualization.
2
Data Visualization
Using charts to visualize data helps with improving understanding of a data set,
interpretation of the data and communications among people who work with it.
Pandas offers many chart that are suitable for use within
management or corporate presentations.
It is important that x-axis will be the index of your DataFrame. If you want to have a
specific column to be your x-axis, then you need to set the index to that column.
3
Comparison of different charts.
Graph Type Advantage Disadvantage
Line graph Can compare multiple Use only with continuous data
A line graph plots continuous data as continuous data sets easily
points and then joins them with a Interim data can be inferred
line. Multiple data sets can be from graph line
graphed together, but a key must be
used.
4
Comparison of different charts.
Graph Type Advantage Disadvantage
Histogram Visually strong Cannot read exact values
A histogram displays continuous Can compare to normal because data is grouped into
data in ordered columns. Categories curve categories
are of continuous measure such as Usually vertical axis is a More difficult to compare two data
time, inches, temperature, etc. frequency count of items sets
falling into each category Use only with continuous data
5 Source:
https://www.whaqualitycenter.org/Portals/0/Tools%20to%20Use/Making%20Sense%20of%20Data/Using%20Graphs%20to%20Display%20Data%20R%202-12.pdf
Plotting a Line Chart
A line graph plots continuous data as points and then joins them with a line. Multiple data sets
can be graphed together, but a key must be used.
Example: A line chart to display number of students enrolled in Undergraduate and Post Graduate
6
Plotting a Bar Chart
A bar graph displays discrete data in separate columns. A double bar graph can be used to
compare two data sets.
7
Plotting a Pie Chart
A pie chart displays data as a percentage of the whole. Each pie section should have a label
and percentage. A total data number should be included.
df['Total Enrolled'].plot.pie()
8
Enhancing the Chart - Example
1. Create a bar chart with title and change colours
Y-label
X-label
9
Summary of Pandas Commands
Display chart
• DataFrame[Column].plot.line()
• DataFrame[Column].plot.bar()
• DataFrame[Column].plot.pie()
Enhance
10