CS158-1L: Artificial Intelligence Laboratory
Machine Problem #3: Exercises
Name: Billones, Sebastian Luise M. Score:
Section: A2 Date: 9/25/2023
Objectives:
● Understand the programming fundamentals and the Python language.
● Write Python programs that utilize variables, data types, and operators.
Instructions:
1. To complete this exercise, please follow the sample commands in Python provided to you.
Once you have completed the assignment, please submit the IPython file and this
document to me. You have one week to complete the exercise from the assigned date.
Please let me know if you have any questions or concerns regarding the assignment.
2. When submitting your completed assignment, please name the IPython file as follows:
"surname_firstname_MP1Exercise". Replace "surname" with your last name, "firstname" with
your first name, and "MP2Exercise" with the name of the machine problem.
For example, if your name is John Smith and the machine problem is "PythonExercise2", the
file name should be "smith_john_PythonExercise1.ipynb".
Please adhere to this naming convention when submitting your file. This will ensure I can
quickly identify your submission and provide appropriate feedback.
Series: The first main data type we will learn about for pandas is the Series data
type. Let's import Pandas and explore the Series object.
A Series is very similar to a NumPy array (in fact it is built on top of the NumPy array
object). What differentiates the NumPy array from a Series, is that a Series can
have axis labels, meaning it can be indexed by a label, instead of just a number
Prepared by: Raymond Sedilla, Mapua University
CS158-1L: Artificial Intelligence Laboratory
Machine Problem #3: Exercises
location. It also doesn't need to hold numeric data, it can hold any arbitrary
Python Object.
In [1] import numpy as np
import pandas as pd
Creating a Series from Python Objects
In [2] help(pd.Series)
Index and Data Lists
We can create a Series from Python lists (also from NumPy arrays)
In [3] myindex = ['USA','Canada','Mexico']
In [4] mydata = [1776,1867,1821]
In [5] myser = pd.Series(data=mydata)
In [6] myser
Out[6] 0 1776
1 1867
2 1821
dtype: int64
In [7] pd.Series(data=mydata,index=myindex)
Out[7] USA 1776
Canada 1867
Mexico 1821
dtype: int64
Built-in Methods to create arrays
In [8] ran_data = np.random.randint(0,100,4)
Prepared by: Raymond Sedilla, Mapua University
CS158-1L: Artificial Intelligence Laboratory
Machine Problem #3: Exercises
In [9] ran_data
Out[9] array([61, 95, 92, 78])
In [10] names = ['Andrew','Bobo','Claire','David']
In [11] ages = pd.Series(ran_data,names)
In [12] ages
Out[12] Andrew 61
Bobo 95
Claire 92
David 78
dtype: int32
From a Dictionary
In [13] ages = {'Sammy':5,'Frank':10,'Spike':7}
In [14] ages
Out[14] {'Sammy': 5, 'Frank': 10, 'Spike': 7}
In [15] pd.Series(ages)
Out[15] Sammy 5
Frank 10
Spike 7
dtype: int64
Key Ideas of a Series
Named Index
In [16] # Imaginary Sales Data for 1st and 2nd Quarters for Global Company
q1 = {'Japan': 80, 'China': 450, 'India': 200, 'USA': 250}
Prepared by: Raymond Sedilla, Mapua University
CS158-1L: Artificial Intelligence Laboratory
Machine Problem #3: Exercises
q2 = {'Brazil': 100,'China': 500, 'India': 210,'USA': 260}
In [17] # Convert into Pandas Series
sales_Q1 = pd.Series(q1)
sales_Q2 = pd.Series(q2)
In [18] sales_Q1
Out[18] Japan 80
China 450
India 200
USA 250
dtype: int64
In [19] # Call values based on Named Index
sales_Q1['Japan']
Out[19] 80
In [20] # Integer Based Location information also retained!
sales_Q1[0]
Out[20]
Operations
In [21] # Grab just the index keys
sales_Q1.keys()
Out[21] Index(['Japan', 'China', 'India', 'USA'], dtype='object')
In [22] # Can Perform Operations Broadcasted across entire Series
sales_Q1 * 2
Out[22] Japan 160
China 900
India 400
USA 500
dtype: int64
Prepared by: Raymond Sedilla, Mapua University
CS158-1L: Artificial Intelligence Laboratory
Machine Problem #3: Exercises
In [23] sales_Q2 / 100
Out[23] Brazil 1.0
China 5.0
India 2.1
USA 2.6
dtype: float64
Between Series
In [24] # Notice how Pandas informs you of mismatch with NaN
sales_Q1 + sales_Q2
Out[24] Brazil NaN
China 950.0
India 410.0
Japan NaN
USA 510.0
dtype: float64
In [25] # You can fill these with any value you want
sales_Q1.add(sales_Q2,fill_value=0)
Out[25] Brazil 100.0
China 950.0
India 410.0
Japan 80.0
USA 510.0
dtype: float64
Prepared by: Raymond Sedilla, Mapua University