CC HHAAP TPE TR E3 R 12
Decision Structures
and Boolean Logic
Pandas
Pandas Introduction
https://www.w3schools.com/python/pandas/pandas_intro.asp
• Pandas is a Python library used for working with data sets.
• It has functions for analyzing, cleaning, exploring, and manipulating data.
• The name "Pandas" has a reference to both "Panel Data", and "Python Data
Analysis" and was created by Wes McKinney in 2008.
• Pandas allows us to analyze big data and make conclusions based on
statistical theories.
• Pandas can clean messy data sets, make them readable and relevant.
• Relevant data is very important in data science.
What Can Pandas do?
Pandas gives you answers about the data.
Like:
• Is there a correlation between two or more columns?
• What's the average, median, max, or min of each column?
• Does column A correlate with column B?
• What does the distribution of data in column C look like?
→ Pandas are also able to delete rows that are not relevant, or contains
wrong values, like empty or NULL values. This is called cleaning the data.
→ Visualize the data with help from Matplotlib. Plot bars, lines, histograms,
etc
→ Store the cleaned, transformed data back into a CSV, other file or
database
Topics
Data Types
Series
Introduce two key
data types in Pandas
DataFrame
Core components of pandas:
Series and DataFrames
A Series is essentially a column, and a DataFrame is a multi-dimensional
table made up of a collection of Series.
Pandas Series - from dictionary
• Pandas is usually imported under the name ‘pd’ alias. (The same way Numpy is imported
under the name ‘np’)
• We can create a Series object from a dictionary
Format : pd.Series({index:values})
The items on the left are referred to as the
index of the Series, and are available as
the index attribute of the series object
Python is storing each value within this column as a 64 bit integer
Pandas Series – add more values
• Regarding data access, the Series behaves like a dictionary:
• Use [ ] and the key as index to add more values to the Series
Assessing information in Series using
<name of series>.loc[ ]
Alternatively, you can also define the dictionary first
and then create the Series
Panda Series - from list
• We can also create a Series object from a list
• The format is pd.Series ([
],index = [
If we omit the index
argument, the Series will
assume an integer index
However, an index can be added subsequently:
])
Create a series of the first list
send, and using index
argument in second list
Plotting Data in Panda Series
Pandas uses the plot() method to
create diagrams.
We can use pyplot, a submodule of
the Matplotlib library to visualize the
diagram on the screen.
The Series.plot()method accepts an
argument kind such as kind="bar", but
there is an equivalent method available
which is: Series.plot.bar()
two options:
<name_of_series>.plot(kind =‘bar’)
<name_of_series>.plot.bar()
Try this!
NAME
Ali
Remy
Aini
MARKS
80
75
84
• create a Series object (you can use dictionary or list) that
contains the information as shown in the table and print them
• Next plot a bar chart to show the data
Sorting the Series and few numerical methods
<name_series>.sort_values()
<name_series>.sort_values(ascending = False)
<name_series>.sort_index()
Sorting the Series and few numerical methods
len() returns the
number of data points
in the Series object
Try this!
NAME
Ali
Remy
Aini
MARKS
80
75
84
• Try to sort the marks and plot the bar
chart again
• Find the average marks and the
number of students
• Write a statement that uses the
average marks and the number of
students
Try this!
NAME
Cylinder 1
Cylinder 2
Cylinder 3
RADIUS HEIGHT
3.4
9.0
7.8
2.4
6.2
6.2
• create a Series object (you can use dictionary or list) that
contains the information as shown in the table and print them
• Next plot a bar chart to show the data
Handling Missing Values
• “Real” data sets tend to be incomplete. Dealing with missing values is an important topic in data
science.
• The agreement in Pandas is that the special floating-point value "NaN" (stand for Not a Number)
represents missing data points. For example, if we have a table for the stock, but we don’t know the
value for apple, we would replace it with NaN.
• The special NaN value in Python can be created using float('nan') or using numpy.nan if the numpy
module is imported
We can also ’tidy up’ the Series object, by
removing all entries that have a NaN value
Creating Pandas DataFrame (using Series)
DataFrame: 2D data structure;
like a 2D array, or a table with
rows and columns.
Create a Pandas DataFrame using Series:
Example:
DataFrame from two series;
Quantity and Price
pd.DataFrame({
‘Index1’: Series1,
‘Index2’: Series2,
… })
Pass a dictionary of Series to DataFrame function()
Accessing data in DataFrame
The data frame has an index which is the same for all columns, and shown in bold in the left
most column. We can also ask for it:
Extract rows of data
Extract columns of data
Data manipulation in DataFrame
Add extra column in dataframe
Adding new column to
an existing DataFrame
Data manipulation in DataFrame (2)
Data manipulation in DataFrame (3)
Creating Pandas DataFrame (using Series from list)
By default, dataframe
index is set as integer
0, 1, 2, 3, …
Creating Pandas DataFrame (using Series from list)
(1) Create stock Series
(2) Create price Series
(3) Put the Series in DataFrame named myRuncit
Data manipulation in DataFrame (2)
Add data in Series
Manipulate the series data and add extra
column in dataframe
Example
Volume of cylinder
• Create a panda Series for radius with these values. [1.5, 2.3, 4.5, 3.1].
Name the index as [‘object1’, ‘object2’, ‘object3’, ‘object4’].
• Print the series and plot a bar chart
• Create a panda Series for height with these values. [7.5, 4.5, 2.8, 5.6]
• Combine the two Series objects into a dataframe named myCylinder. Use
index name [‘RADIUS’, ‘HEIGHT’]. Print the dataframe
• Calculate the volume for every cylinder. Add the volume as another column in
myCylinder dataframe. Display the updated data in myCylinder
• Plot a sorted bar chart based on volume.
• Plot a bar chart showing radius, height and volumes in a single graph.
Try this!
NAME
Height
Top radius, r1
Middle radius, r2
Barrel 1
8.5
2.5
4.1
Barrel 2
4.7
5.8
6.8
Barrel 3
6.9
3.6
5.4
• create Series object of height, top radius, and middle radius
(you can use dictionary or list). Display and plot the Series
• Create a DataFrame named dfCylinder contains all the
series
• Calculate the volume, and add to the dataframe. Set to 2
decimal places. Display the updated dataframe
• Next, plot a bar chart to show the data (sort the volume)