KEMBAR78
Python Unit-4 Notes | PDF | Matrix (Mathematics) | Computer Programming
0% found this document useful (0 votes)
9 views44 pages

Python Unit-4 Notes

NumPy is a Python library for efficient array manipulation and mathematical operations, created in 2005. It provides an array object called ndarray that is significantly faster than traditional Python lists and supports various dimensions and operations. The document covers how to create, index, slice, and perform arithmetic operations on NumPy arrays.

Uploaded by

sanam7122004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views44 pages

Python Unit-4 Notes

NumPy is a Python library for efficient array manipulation and mathematical operations, created in 2005. It provides an array object called ndarray that is significantly faster than traditional Python lists and supports various dimensions and operations. The document covers how to create, index, slice, and perform arithmetic operations on NumPy arrays.

Uploaded by

sanam7122004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 44

UNIT-4

NumPy
NumPy is a Python library used for working with arrays. It also has
functions for working in domain of linear algebra, fourier transform,
and matrices. NumPy was created in 2005 by Travis Oliphant. It is an
open source project and you can use it freely. NumPy stands for
Numerical Python.
In Python we have lists that serve the purpose of arrays, but they are
slow to process. NumPy aims to provide an array object that is up to
50x faster than traditional Python lists.
The array object in NumPy is called ndarray, it provides a lot of
supporting functions that make working with ndarray very easy.

Once NumPy is installed, import it in your applications by adding


the import keyword:

import numpy

Now NumPy is imported and ready to use.

Example
import numpy
arr = numpy.array([1, 2, 3, 4, 5]) # array() is used to create an array
print(arr)
NumPy is usually imported under the np alias.
alias: In Python alias are an alternate name for referring to the same
thing.
Create an alias with the as keyword while importing:
import numpy as np

Example:

import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)

Checking NumPy Version


The version string is stored under __version__ attribute.
Example
import numpy as np
print(np.__version__)

NumPy Creating Arrays


To create an ndarray, we can pass a list, tuple or any array-like object
into the array() method, and it will be converted into an ndarray:

Dimensions in Arrays
A dimension in arrays is one level of array depth (nested arrays).
0-D Arrays
0-D arrays, or Scalars, are the elements in an array. Each value in an
array is a 0-D array.
Example Output:
Create a 0-D array with value 42 42
import numpy as np

arr = np.array(42)

print(arr)

1-D Arrays
An array that has 0-D arrays as its elements is called uni-dimensional or
1-D array.
These are the most common and basic arrays.
Example Output:
Create a 1-D array containing the [1 2 3 4 5]
values 1,2,3,4,5:

import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)

2-D Arrays
An array that has 1-D arrays as its elements is called a 2-D array.
These are often used to represent matrix or 2nd order tensors.
Example Output:
Create a 2-D array containing
two arrays with the values 1,2,3 [[ 1 2 3]
and 4,5,6: [4 5 6]]

import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr)

3-D arrays
An array that has 2-D arrays (matrices) as its elements is called 3-D
array.
These are often used to represent a 3rd order tensor.
Example Output:
Create a 3-D array with two 2-D
[[[1 2 3]
arrays, both containing two
[4 5 6]]
arrays with the values 1,2,3 and
4,5,6: [[1 2 3]
import numpy as np [4 5 6]]]

arr = np.array([[[1, 2, 3], [4, 5, 6]],


[[1, 2, 3], [4, 5, 6]]])

print(arr)
To Check Number of Dimensions?
NumPy Arrays provides the ndim attribute that returns an integer that
tells us how many dimensions the array has.
Example: Output:
Program to check how many
0
dimensions the arrays have:
1
import numpy as np 2
3
a = np.array(42)
b = np.array([1, 2, 3, 4, 5])
c = np.array([[1, 2, 3], [4, 5, 6]])
d = np.array([[[1, 2, 3], [4, 5, 6]],
[[1, 2, 3], [4, 5, 6]]])

print(a.ndim)
print(b.ndim)
print(c.ndim)
print(d.ndim)

Higher Dimensional Arrays


An array can have any number of dimensions.
When the array is created, you can define the number of dimensions by
using the ndmin argument.
Example Output:
Create an array with 5
dimensions and verify that it has [[[[[1 2 3 4 ]]]]]
5 dimensions: number of dimensions : 5

import numpy as np

arr = np.array([1, 2, 3, 4],


ndmin=5)

print(arr)
print('number of dimensions :',
arr.ndim)

Indexing Arrays (Array Indexing)


Access Array Elements
Array indexing is the same as accessing an array element (element in a
list) by referring to its index number.
The indexes in NumPy arrays start with 0, meaning that the first
element has index 0, and the second has index 1 etc.
Example 1 Output:
Get the first element from the 1
following array:
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr[0])
Example 2 Output:
Get the second element from the 2
following array.
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr[1])

Example 3 Output:
Get third and fourth elements 7
from the following array and add
them.

import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr[2] + arr[3])

Access 2-D Arrays


To access elements from 2-D arrays we can use comma separated
integers representing the dimension and the index of the element.
Example Output:
2
Access the 2nd element on 1st
dim:
import numpy as np
arr = np.array([[1,2,3,4,5],
[6,7,8,9,10]])
print('2nd element on 1st dim: ',
arr[0, 1])
Example Output:
10
Access the 5th element on 2nd
dim:
import numpy as np
arr = np.array([[1,2,3,4,5],
[6,7,8,9,10]])
print('5th element on 2nd dim: ',
arr[1, 4])

Access 3-D Arrays


To access elements from 3-D arrays we can use comma separated
integers representing the dimensions and the index of the element.
Example OUTPUT:
6
Access the third element of the
second array of the first array:
import numpy as np
arr = np.array([[[1, 2, 3], [4, 5, 6]],
[[7, 8, 9], [10, 11, 12]]])
print(arr[0, 1, 2])

Explanation:
arr[0, 1, 2] prints the value 6.
And this is why:
The first number represents the first dimension, which contains two
arrays:
[[1, 2, 3], [4, 5, 6]]
and:
[[7, 8, 9], [10, 11, 12]]
Since we selected 0, we are left with the first array:
[[1, 2, 3], [4, 5, 6]]
The second number represents the second dimension, which also
contains two arrays:
[1, 2, 3]
and:
[4, 5, 6]
Since we selected 1, we are left with the second array:
[4, 5, 6]
The third number represents the third dimension, which contains three
values:
4
5
6
Since we selected 2, we end up with the third value:
6

Write a Python program to perform basic arithmetic operations using


1-D array.

import numpy as np
arr=np.array([1,3,5,7,9])
sum=arr[0]+arr[2]
diff=arr[3]-arr[2]
product=arr[1]*arr[3]
div=arr[4]/arr[1]
print("sum of 2 numbers=",sum)
print("Difference of 2 numbers=",diff)
print("Product of 2 numbers=",product)
print("Division of 2 numbers=",div)

Write a Python program to perform basic arithmetic operations using


2-D array.

import numpy as np
arr=np.array([[1,3,5,7,9],[11,13,15,17,19]])
sum=arr[0,1]+arr[1,1]
diff=arr[1,3]-arr[0,2]
product=arr[1,1]*arr[0,3]
div=arr[1,2]/arr[0,2]
print("sum of 2 numbers=",sum)
print("Difference of 2 numbers=",diff)
print("Product of 2 numbers=",product)
print("Division of 2 numbers=",div)

Negative Indexing
Use negative indexing to access an array from the end.
Example OUTPUT:
Last element from 2nd dim: 10
Print the last element from the
2nd dim:
import numpy as np
arr = np.array([[1,2,3,4,5],
[6,7,8,9,10]])
print('Last element from 2nd dim:
', arr[1, -1])

NumPy Array Slicing (Slicing arrays)


Slicing in python means taking elements from one given index to
another given index.
We pass slice instead of index like this: [start:end].
We can also define the step, like this: [start:end:step].
If we don't pass start its considered 0. If we don't pass end its
considered length of array in that dimension. If we don't pass step its
considered 1.
Example: OUTPUT:

import numpy as np
arr = np.array([2,4,6,8,10,12,14,16])
print(arr[3:6]) # will print the elements from 3 to 5 [ 8 10 12]
print(arr[:5]) # will print the elements from 0 to 4 [ 2 4 6 8 10]
print(arr[2:]) # will print the elements from 2 to [ 6 8 10 12 14
length of the array. 16]
print(arr[-5:-1]) # will print from the end i.e. -5 to - [ 8 10 12 14]
2 [ 2 4 6 8 10 12
print(arr[:-1]) # will print from end i.e. 0 to -2 14]

STEP
Use the step value to determine the step of the slicing:
Example: Output:
Return every other element from index 1 to
[2 4]
index 5:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[1:5:2])

Slicing 2-D Arrays


Example 1: Output:
From the second element, slice elements from
index 1 to index 4 (not included):

import numpy as np [ 7 8 9]
arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[1, 1:4])
Example 2: Output:
From both elements, return index 2:

import numpy as np
arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]) [ 3 8]
print(arr[0:2, 2])

Example 3: Output:
From both elements, slice index 1 to index 4
(not included), this will return a 2-D array:

import numpy as np [[ 2 3 4]
arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]) [7 8 9]]
print(arr[0:2, 1:4])

NumPy Array Copy vs View


The main difference between a copy and a view of an array is that the
copy is a new array, and the view is just a view of the original array.
The copy owns the data and any changes made to the copy will not
affect original array, and any changes made to the original array will not
affect the copy.
The view does not own the data and any changes made to the view will
affect the original array, and any changes made to the original array will
affect the view.
Example: copy() Example: view()
Make a copy, change the original Make a view, change the original
array, and display both arrays: array, and display both arrays:

import numpy as np import numpy as np

arr = np.array([1, 2, 3, 4, 5]) arr = np.array([1, 2, 3, 4, 5])


x = arr.copy() x = arr.view()
arr[0] = 42 arr[0] = 42

print(arr) print(arr)
print(x) print(x)

OUTPUT: OUTPUT:
[42 2 3 4 5] [42 2 3 4 5]
[1 2 3 4 5] [42 2 3 4 5]

NumPy Array Shape


ndarray.shape : This array attribute returns a tuple consisting of array
dimensions. It can also be used to resize the array.
Example 1: Output
(2, 3)
import numpy as np
a=np.array([[1,2,3],[4,5,6]])
print(a.shape)
Example 2: Resizing the array: Output
[[1 2]
import numpy as np [3 4]
a=np.array([[1,2,3],[4,5,6]]) [5 6]]
a.shape=(3,2)
print(a)
NumPy Array Reshaping
Reshaping means changing the shape of an array. The shape of an array
is the number of elements in each dimension. By reshaping we can add
or remove dimensions or change number of elements in each
dimension.
Example 1: Output

Convert the following 1-D array with 12 elements


into a 2-D array. [[1 2 3]
The outermost dimension will have 4 arrays, each [4 5 6]
with 3 elements: [7 8 9]
[10 11 12]]
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
newarr = arr.reshape(4, 3)
print(newarr)

Example 2: Output

Convert the following 1-D array with 12 elements


into a 3-D array. [[[1 2]
The outermost dimension will have 2 arrays that [3 4]
contains 3 arrays, each with 2 elements: [5 6]]
[[ 7 8]
import numpy as np [9 10]
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) [11 12]]]
newarr = arr.reshape(2, 3, 2)
print(newarr)
np.arange() | NumPy Arange Function in Python
numpy.arange() is an inbuilt numpy function that returns an ndarray
object containing evenly spaced values within a defined interval. For
instance, you want to create values from 1 to 10; you can use
numpy.arange() function.

Syntax:

numpy.arange(start, stop, step)

 Start: Start of interval


 Stop: End of interval
 Step: Spacing between values. Default step is 1

Example 1: Output:

import numpy as np array([ 1, 2, 3, 4, 5, 6, 7,


np.arange(1, 11) 8, 9, 10])

Example 2: Output:

If you want to change the step, you can array([ 1, 5, 9, 13])


add a third number in the parenthesis.
It will change the step.

import numpy as np
np.arange(1, 14, 4)

ndarray.ndim

This array attribute returns the number of array dimensions.


Example: Output:

# this is one dimensional array 1


import numpy as np
a = np.arange(24) [[[ 0, 1, 2]
print(a.ndim ) [ 3, 4, 5]
[ 6, 7, 8]
# now reshape it [ 9, 10, 11]]
b = a.reshape(2,4,3) [[12, 13, 14]
print b # b is having [15, 16, 17]
three dimensions [18, 19, 20]
[21, 22, 23]]]

Matrix Operation using Numpy.Array() (ARRAY Processing)

Matrix Addition
To perform addition on the matrix, we will create two matrices using
numpy.array() and add them using the (+) operator.

Example: Output:

import numpy as np [[ 12 -12 36]


[ 16 12 48]
M1 = np.array([[3, 6, 9], [5, -10, 15], [-7, [ 6 -12 60]]
14, 21]])
M2 = np.array([[9, -18, 27], [11, 22, 33],
[13, -26, 39]])
M3 = M1 + M2
print(M3)

Matrix Subtraction
To perform subtraction on the matrix, we will create two matrices using
numpy.array() and subtract them using the (-) operator.

Example: Output:

import numpy as np [[ -6 24 -18]


[ -6 -32 -18]
M1 = np.array([[3, 6, 9], [5, -10, 15], [-7, [-20 40 -18]]
14, 21]])
M2 = np.array([[9, -18, 27], [11, 22, 33],
[13, -26, 39]])
M3 = M1 - M2
print(M3)

Matrix Multiplication
First will create two matrices using numpy.arary(). To multiply them,
you can make use of numpy.dot() method. Numpy.dot() is the dot
product of matrix M1 and M2. Numpy.dot() handles the 2D arrays and
perform matrix multiplications.

Example: Output:

import numpy as np [[ 93 78]


[ -65 -310]]
M1 = np.array([[3, 6], [5, -10]])
M2 = np.array([[9, -18], [11, 22]])
M3 = M1.dot(M2)
print(M3)

Matrix Transpose
The transpose of a matrix is calculated, by changing the rows as
columns and columns as rows. The transpose() function from Numpy
can be used to calculate the transpose of a matrix.

Example 1: Output:

import numpy as np [[ 3 5 4]
[ 6 -10 8]
M1 = np.array([[3, 6, 9], [5, -10, 15], [ 9 15 12]]
[4,8,12]])
M2 = M1.transpose()

print(M2)

Example 2: Output:

import numpy as np [[0 1]


a = np.arange(6).reshape((3,2)) [2 3]
print(a) [4 5]]
np.transpose(a)
array([[0, 2, 4],
[1, 3, 5]])

To print specified rows and columns


If the user wants to print any specified number of rows and columns,
then he can use the concept of slicing.
Example: Consider an array,

M1 = np.array([[2,4,6,8,10], [3,6,9,-12,-15], [4,8,12,16,-20], [5,-10,15,-


20,25]])

In the above array we have total 4 rows ( starting from 0,1,2,3) and 5
(starting from 0,1,2,3,4) columns i.e.,

print(M1)

[[2,4,6,8,10]
[3,6,9,-12,-15]
[4,8,12,16,-20]
[5,-10,15,-20,25]

To print row number 1 and 2 and column 1,2,3 then the statement we
use is,
print(M1[1:3,1:4])

To print starting 3 rows and starting 2 columns


print(M1[:3,:2])

Array Input and Output

Matrix is nothing but a rectangular arrangement of data or numbers.


The horizontal entries in a matrix are called as ‘rows’ while the vertical
entries are called as ‘columns’. If a matrix has r number of rows and c
number of columns, then the order of matrix is given by r x c. Each
entry in a matrix can be integer values, or floating values, or even it can
be complex numbers.
"Arr_1" is an integer array of size M x N. Size and content of the array
is to be read using input() by using the user defined function
READ_DATA(). It is required to display the
i) diagonal elements of "Arr_1"
ii) elements of mth row ( row no should be entered by user)
iii) elements of nth column (column no should be entered by user)

def READ_DATA(R,C,matrix):
for i in range(m):
for j in range(n):
matrix[i][j]=int(input())
import numpy
m= int(input("\n Enter the row size of M "))
n=int(input("\nEnter the columnn size of N"))
matrix=numpy.ndarray(shape=(m,n), dtype=int)
print("\n size:",matrix.size)
print("\n shape: ",matrix.shape)
print("\nDimensions:",matrix.ndim)
print("\n Enter %d elements of %dx%d matrix" %(m*n,m,n))
READ_DATA(m,n,matrix)
print("\n %dx%d matrix are:"%(m,n))
print(matrix)
print("Diagnol Elements of Matrix :")
dia=matrix.diagonal()
print(dia)
r=int(input("enter row number to display"))
print(matrix[r])
c=int(input("Enter Col number to display"))
print(matrix[:,c])
Iterating Arrays
• Iterating means going through elements one by one.
• As we deal with multi-dimensional arrays in numpy, we can do
this using basic for loop of python.
• If we iterate on a 1-D array it will go through each element one by
one

Example :

Iterating 1-D Arrays

import numpy as np
arr = np.array([1, 2, 3])
for x in arr:
print(x)

Iterating 2-D Arrays


In a 2-D array it will go through all the rows.
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
for x in arr:
print(x)

Iterating 3-D Arrays


import numpy as np
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
for x in arr:
print(x)

Universal array function

What are ufuncs?


ufuncs stands for "Universal Functions" in NumPy are simple
mathematical functions.
It is a term that we gave to mathematical functions in numpy library.
Numpy provides various universal functions that cover wide variety of
operations.

Some of the universal functions are:


1. Trigonometric functions
2. Statistical functions
3. Bit-twiddling functions

Example: OUTPUT:

import numpy as np Sine values of array


elements:
# create an array of sine values [ 0.00000000e+00
a = np.array([0, np.pi/2, np.pi]) 1.00000000e+00
print ("Sine values of array 1.22464680e-16]
elements:", np.sin(a)) Exponent of array
elements: [ 1.
# exponential values 2.71828183 7.3890561
a = np.array([0, 1, 2, 3]) 20.08553692]
print ("Exponent of array Square root of array
elements:", np.exp(a)) elements: [ 0.
1. 1.41421356
# square root of array values 1.73205081]
print ("Square root of array
elements:", np.sqrt(a))
How do you perform array manipulation in NumPy, such as
i) Reshaping
ii) Concatenating along rows
iii) splitting arrays along columns
iv) flatten
v) transpose

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6])


reshaped_arr = np.reshape(arr, (2, 3))
print(reshaped_arr)

arr1 = np.array([[1, 2], [3, 4]])


arr2 = np.array([[5, 6], [7, 8]])
concatenated_arr = np.concatenate((arr1, arr2), axis=0) # Concatenate along rows (vertical)
print(concatenated_arr)
concatenated_arr = np.concatenate((arr1, arr2), axis=1) # Concatenate along columns
(horizontal)
print(concatenated_arr)

arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
split_arr = np.split(arr, 3, axis=1) # Split into 3 arrays along columns (horizontal)
print(split_arr)

arr = np.array([[1, 2, 3], [4, 5, 6]])


transposed_arr = arr.transpose()
print(transposed_arr)
flattened_arr = arr.flatten()
print(flattened_arr)

Assignment Question-1:

Illustrate a Numpy program to test element-wise for complex number, real number of a given
array. Also test whether a given number is a scalar type or not.

Pandas
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.

Why Use Pandas?


Pandas allows us to analyze big data and make conclusions based on
statistical theories.
Pandas can clean messy data sets, and make them readable and
relevant.
Relevant data is very important in data science.

Data Science is a branch of computer science where we study how to


store, use and analyze data for deriving information from it.

What Can Pandas Do?


Pandas gives you answers about the data. Like:
 Is there a correlation between two or more columns?
 What is average value?
 Max value?
 Min value?

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.

Once Pandas is installed, import it in your applications by adding the


import keyword:

import pandas

Now Pandas is imported and ready to use.

Example
import pandas

mydataset = {
'cars': ["BMW", "Volvo", "Ford"],
'passings': [3, 7, 2]
}

myvar = pandas.DataFrame(mydataset)

print(myvar)

Create an alias with the as keyword while importing:

import pandas as pd

Now the Pandas package can be referred to as pd instead of pandas.

Example
import pandas as pd

mydataset = {
'cars': ["BMW", "Volvo", "Ford"],
'passings': [3, 7, 2]
}

myvar = pd.DataFrame(mydataset)

print(myvar)
Checking Pandas Version
The version string is stored under __version__ attribute.
Example
import pandas as pd

print(pd.__version__)

What is a Series?
A Pandas Series is like a column in a table.
It is a one-dimensional array holding data of any type.

Example
Create a simple Pandas Series from a list:

import pandas as pd

a = [1, 7, 2]

myvar = pd.Series(a)

print(myvar)

Labels
If nothing else is specified, the values are labeled with their index
number. First value has index 0, second value has index 1 etc.
This label can be used to access a specified value.
Example
Return the first value of the Series:
print(myvar[0])

Create Labels
With the index argument, you can name your own labels.
Example
Create you own labels:
import pandas as pd

a = [1, 7, 2]

myvar = pd.Series(a, index = ["x", "y", "z"])

print(myvar)
When you have created labels, you can access an item by referring to
the label.

Example
Return the value of "y":
print(myvar["y"])

Key/Value Objects as Series


You can also use a key/value object, like a dictionary, when creating a
Series.
Example
Create a simple Pandas Series from a dictionary:
import pandas as pd

calories = {"day1": 420, "day2": 380, "day3": 390}


myvar = pd.Series(calories)

print(myvar)
Note: The keys of the dictionary become the labels.
To select only some of the items in the dictionary, use the index
argument and specify only the items you want to include in the Series.
Example
Create a Series using only data from "day1" and "day2":
import pandas as pd

calories = {"day1": 420, "day2": 380, "day3": 390}

myvar = pd.Series(calories, index = ["day1", "day2"])

print(myvar)

DataFrames
Data sets in Pandas are usually multi-dimensional tables, called
DataFrames.
Series is like a column, a DataFrame is the whole table.
Example
Create a DataFrame from two Series:
import pandas as pd

data = {
"calories": [420, 380, 390],
"duration": [50, 40, 45]
}
myvar = pd.DataFrame(data)

print(myvar)

What is a DataFrame?
A Pandas DataFrame is a 2 dimensional data structure, like a 2
dimensional array, or a table with rows and columns.
Example
Create a simple Pandas DataFrame:
import pandas as pd

data = {
"calories": [420, 380, 390],
"duration": [50, 40, 45]
}

#load data into a DataFrame object:


df = pd.DataFrame(data)

print(df)

Result

calories duration
0 420 50
1 380 40
2 390 45
Locate Row
As you can see from the result above, the DataFrame is like a table with
rows and columns.
Pandas use the iloc attribute to return one or more specified row(s)
Example
Return row 0:
#refer to the row index:
print(df.iloc[0])

Result

calories 420
duration 50
Name: 0, dtype: int64

Note: This example returns a Pandas Series.


Example
Return row 0 and 1:
#use a list of indexes:
print(df.iloc[[0, 1]])

Result

calories duration
0 420 50
1 380 40

Note: When using [], the result is a Pandas DataFrame.


Named Indexes
With the index argument, you can name your own indexes.
Example
Add a list of names to give each row a name:
import pandas as pd

data = {
"calories": [420, 380, 390],
"duration": [50, 40, 45]
}

df = pd.DataFrame(data, index = ["day1", "day2", "day3"])

print(df)

Result

calories duration
day1 420 50
day2 380 40
day3 390 45

Locate Named Indexes


Use the named index in the loc attribute to return the specified row(s).
Example
Return "day2":
#refer to the named index:
print(df.loc["day2"])

Result

calories 380
duration 40
Name: 0, dtype: int64

To produce multiple summary statistics in one shot, we use

df.describe( )

To calculate the mean of the dataframe calculated for rows, for


columns

df.mean(axis=’rows’)
df.mean(axis=’columns’)

To display correlation and covariance between different columns of


the same table.
df.calories.corr(df.duration)
df.calories.cov(df.duration)

To List the unique values of the series and count the value
frequencies.

df.unique( )
df.value_counts()

To Calculate the correlation between two different tables.

df1.corrwith(df2)

Assignment Question-1:

Given a student dataframe created with the following structure, show


the following operations:
ID Age Gender Sub_Score
1 20 M 85
2 21 F 90
3 22 M 78
4 23 F 82
5 24 M 88

1. Show the creation of a dataframe.


2. Display rows 2 to 5.
3. Split the dataset which belongs to Female gender, Show the
resulting datasets.
4. Access rows where Sub_Score is greater than or equal to 85. Print
the resulting rows.

Load Files into a DataFrame


If your data sets are stored in a file, Pandas can load them into a
DataFrame.
Example
Load a comma separated file (CSV file) into a DataFrame:
import pandas as pd

df = pd.read_csv('data.csv')

print(df)

Data Visualization:
In today’s world, a lot of data is being generated daily. And sometimes
to analyze this data for certain trends, patterns may become difficult if
the data is in its raw format. To overcome this data visualization comes
into play. Data visualization provides a good, organized pictorial
representation of the data which makes it easier to understand,
observe, and analyze.

Python provides various libraries that come with different features for
visualizing data. All these libraries come with different features and can
support various types of graphs.

matplotlib:

Matplotlib is a powerful plotting library in Python used for creating


static, animated, and interactive visualizations. Matplotlib’s primary
purpose is to provide users with the tools and functionality to
represent data graphically, making it easier to analyze and understand.
It was originally developed by John D. Hunter in 2003 and is now
maintained by a large community of developers.

Key Features of Matplotlib:


1. Versatility: Matplotlib can generate a wide range of plots,
including line plots, scatter plots, bar plots, histograms, pie
charts, and more.
2. Customization: It offers extensive customization options to
control every aspect of the plot, such as line styles, colors,
markers, labels, and annotations.
3. Integration with NumPy: Matplotlib integrates seamlessly
with NumPy, making it easy to plot data arrays directly.
4. Publication Quality: Matplotlib produces high-quality plots
suitable for publication with fine-grained control over the plot
aesthetics.
5. Extensible: Matplotlib is highly extensible, with a large
ecosystem of add-on toolkits and extensions like Seaborn,
Pandas plotting functions, and Basemap for geographical
plotting.
6. Cross-Platform: It is platform-independent and can run on
various operating systems, including Windows, macOS, and
Linux.
7. Interactive Plots: Matplotlib supports interactive plotting
through the use of widgets and event handling, enabling users
to explore data dynamically.

Graphs:

1. Scatter Plot: A scatter plot (aka scatter chart, scatter graph) uses
dots to represent values for two different numeric variables. The
position of each dot on the horizontal and vertical axis indicates
values for an individual data point. Scatter plots are used to
observe relationships between variables.

Here, we use a dataset or csv file called “Test.csv”


downloaded from Kaggle.
Program Code:

import pandas as pd
import matplotlib.pyplot as plt

weather = pd.read_csv('D:/Test.csv')

plt.scatter(weather['humidity'], weather['temperature'])

# Adding Title to the Plot


plt.title("Scatter Plot")

# Setting the X and Y labels


plt.xlabel('humidity')
plt.ylabel('temperature')

plt.show()

2. Bar Chart
A bar plot or bar chart is a graph that represents the category of data
with rectangular bars with lengths and heights that is proportional to
the values which they represent. It can be created using the bar()
method.
Program Code:

import pandas as pd
import matplotlib.pyplot as plt

weather = pd.read_csv('D:/Test.csv')

plt.bar(weather['humidity'], weather['temperature'])

# Adding Title to the Plot


plt.title("Bar Plot")

# Setting the X and Y labels


plt.xlabel('humidity')
plt.ylabel('temperature')

plt.show()

3. Histogram
A histogram is basically used to represent data in the form of some
groups. It is a type of bar plot where the X-axis represents the bin
ranges while the Y-axis gives information about frequency. The hist()
function is used to compute and create a histogram. In histogram, if we
pass categorical data then it will automatically compute the frequency
of that data i.e. how often each value occurred.

Program Code:

import pandas as pd
import matplotlib.pyplot as plt

# reading the database


weather = pd.read_csv('D:/Test.csv')

plt.hist(weather['temperature'])

plt.title("Histogram")

# Adding the legends


plt.show()

4. Line Plot
Line Plot in Seaborn plotted using the lineplot() method. In this, we can
pass only the data argument also.
Program Code:

import seaborn as sns


import matplotlib.pyplot as plt
import pandas as pd

# reading the database


weather = pd.read_csv('D:/Test.csv')

sns.lineplot(x='humidity', y='temperature', data=weather)


plt.show()
We can also draw a line by specifying 2 coordinates.

Program Code:

import matplotlib.pyplot as plt


import numpy as np

xpoints = np.array([0, 6])


ypoints = np.array([0, 250])
plt.plot(xpoints, ypoints)
plt.show()

5. Pie plot
A Pie Chart is a circular statistical plot that can display only one series
of data. The area of the chart is the total percentage of the given data.
Pie charts are commonly used in business presentations like sales,
operations, survey results, resources, etc. as they provide a quick
summary.

Program Code:

import matplotlib.pyplot as plt


import numpy as np

y = np.array([35, 25, 25, 15])


mylabels = ["Apples", "Bananas", "Cherries", "Dates"]

plt.pie(y, labels = mylabels)


plt.show()

Assignment Question:
As a part of skill development program planning to offer one
of the programming language Python, C++, Java, Nodejs,
Reactjs. A google form has been circulated to students to
know which course has demand. Consider total student
strength consider as 250. Assume other values. Make use of
pie chart to represent the percentage of students opted for
each course.

You might also like