1 of 17

Topic 6 :Data Analysis and Visualisation

Learning Outcomes:

​

(a) Identify basic Pandas methods for data analysis (indexing, describe(), max(), min() and mean())

CP125

2 of 17

Topic 6 :Data Analysis and Visualisation

Table of Contents

  • Information about dataset
  • Explore Iris Dataset
  • Indexing
  • Indexing Operator
  • Selecting column/columns with dot and bracket Operator

​

CP125

3 of 17

iris.csv

Datasource for Analysis

4 of 17

Information about datasets

​

​

  • 5 columns
  • 3 Species

5 of 17

  1. Get to know

the datasets using info() method

Datasets contain:

  • 6 columns
  • columns 0-4 are numerical columns.
  • 150 rows

Use own file directory

Indicates whether there is an empty data or net

6 of 17

2. View the first 5 rows

7 of 17

3. Indexing from DataFrame

Indexing a pandas dataframe means selecting particular subsets of data (such as rows, columns, individual cells) from that dataframe

​

​

​

a. selecting specific columns

b. selecting specific rows

c. selecting specific rows and column

8 of 17

Indexing

Ways to Indexing

Native Python Accessor

Column Selection Only

Pandas DataFrame Method

Column and Row Selection

Integer Based Indexing

Label based Indexing

Indexing Operator

Single and Multiple Column

Attribute accessor

Single Column Only

dot Operator

[ ] bracket Operator

loc[row_label,col_label]

iloc[row_indices,col_indices]

3a

3b & 3c

9 of 17

3a.1 dot operator

​

select single Column data based on header name

10 of 17

3a.2

​

[ ] operator

select Single Column data based on header name

11 of 17

3a.3

​

[ ] operator

​

Selecting

Multiple Columns

To select multiple columns, the columns name is passed as a list.

​

12 of 17

3b. Using Pandas loc() method

Use to select rows and columns from a DataFrame using their labels.

How does loc work?

  • For rows: You can specify the row or integer index label(s) you want to select.
  • For columns: You specify the column label(s) you want to select

df.loc[row_labels,column_labels]

13 of 17

3b.1 Selecting one row

To select a single row, passed the row index value.

​

eg: row 100

It return single record with index of 100

​

14 of 17

3b.2

Selecting

multiple rows

pass the row

start_index : end_index

to the loc operator

​

eg: loc[10:100]

It return the record with index of 10 until 100 inclusive .

[10:100] return 91 rows

​

15 of 17

3c.1

Selecting

multiple rows

of specific columns

It return the record with index of

10 until 100 inclusive

and only columns

​

  1. SepalLengthCm
  2. SepealWidthCm

​

16 of 17

3c.2

Selecting specific rows

and columns using

relational/comparison operators

return

  • SepalLengthCm
  • SepalWidthCm
  • Species

​

that has SepalLengthCm > 5.0

​

17 of 17

Summary

  • Indexing can create a subset of the dataframe
  • Dot Operator could be used to select a column
  • Bracket operator could be used select single and multiple columns.
  • Using loc[ ] to select specific rows and columns using the index and columns label