Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
52 views

Data Handling Using Pandas and Data Visualization - Assessment1 Class Room Notes

The document discusses Pandas, an open-source Python library used for data analysis and manipulation. It provides an overview of Pandas, describing what it is used for, who created it, how to import it, and some of its key features. It also provides examples of creating Pandas Series objects with different data types like integers, floats, strings and dictionaries. Finally, it compares the differences between Series and DataFrames, the two fundamental data structures in Pandas.

Uploaded by

Rohan Gamer
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Data Handling Using Pandas and Data Visualization - Assessment1 Class Room Notes

The document discusses Pandas, an open-source Python library used for data analysis and manipulation. It provides an overview of Pandas, describing what it is used for, who created it, how to import it, and some of its key features. It also provides examples of creating Pandas Series objects with different data types like integers, floats, strings and dictionaries. Finally, it compares the differences between Series and DataFrames, the two fundamental data structures in Pandas.

Uploaded by

Rohan Gamer
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Unit No.

I Data Handling using Pandas and Data Visualization Class Room Notes

Chapter No. 1
Python Pandas - I
Python Pandas is Python’s Library

Pandas has derived its name from “Panel Data System”

Data Analysis refers to process of evaluating big data sets using analytical and statistical tools so as to
discover useful information and conclusions to support business decision making.

Author of Pandas is Wes Mckinney

Pandas is open source, BSD library built for python programming language.

Pandas offers high-performance, easy to use data structures and data analysis tools

To work with pandas in python we need to import pandas library in your python environment

Method for importing pandas library is by writing


import pandas as pd

Features of Pandas
1) It can read or write in many different data formats (integer, float, double, string etc.,)
2) It can calculate all the possible ways data is organized i.e. across rows and down columns
3) It can easily select subset of data from bulky data sets and even combine multiple datasets
together. It has functionality to find and fill missing data.
4) It allows you to apply operations to independent groups within the data.
5) It supports reshaping of data into different forms.
6) It supports advanced times-series functionality ( Time series forecasting is the use of a model to
predict future value based on previously observed values)
7) It supports visualization by integrating matplotlib and seaborn etc., libraries

Note : Pandas is best at handling huge tabular data sets comprising different data formats.

There are many more other types of data structures suited for different types of functionality. Out of many
data structures of pandas two basic data structures – Series and Dataframes are universally popular for
their dependability. Another datastructure panel is also there, but we are not going to study panel.
-----------------------------------------------------------------------------------------------------------------------------------------------
# Program No.1
# Program to create an empty series
import pandas as pd
obj1=pd.Series()
print(obj1)
-----------------------------------------------------------------------------------------------------------------------------------------------
Output:
Series([], dtype: float64)
-----------------------------------------------------------------------------------------------------------------------------------------------

Email Id :- mirza.baig@oakridge.in  6300375851 9849104296 Page No :- 1 / 18


Unit No. I Data Handling using Pandas and Data Visualization Class Room Notes
-----------------------------------------------------------------------------------------------------------------------------------------------
# Program No.2
# Program to create a series using range()
import pandas as pd
obj1=pd.Series(range(3))
print (obj1)
-----------------------------------------------------------------------------------------------------------------------------------------------
Output:
0 0
1 1
2 2
dtype: int64
-----------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------
# Program No. 3
# Program to create a series of integer values
import pandas as pd
obj2=pd.Series([2,50,8,90])
print(obj2)
-----------------------------------------------------------------------------------------------------------------------------------------------
Output:
0 2
1 50
2 8
3 90
dtype: int64
-----------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------
# Program No. 4
# Program to create a series of float values
import pandas as pd
obj2=pd.Series([2.5,8.909,12,90.7687])
print(obj2)
-----------------------------------------------------------------------------------------------------------------------------------------------
Output:
0 2.5000
1 8.9090
2 12.0000
3 90.7687
dtype: float64
-----------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------
# Program No. 5(a)
# Program to create a series with mixed data type elements
import pandas as pd
obj3=pd.Series([5,2,7.8,9.4,"sameer"])
print(obj3)
-----------------------------------------------------------------------------------------------------------------------------------------------

Email Id :- mirza.baig@oakridge.in  6300375851 9849104296 Page No :- 2 / 18


Unit No. I Data Handling using Pandas and Data Visualization Class Room Notes
Output:
0 5
1 2
2 7.8
3 9.4
4 sameer
dtype: object
-----------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------
# Program No. 5(b)
# To create a Series of object using individual character 'a','e','i','o','u'
import pandas as abcd
s3=abcd.Series(['a','e','i','o','u'])
print("Series Object")
print(s3)
-----------------------------------------------------------------------------------------------------------------------------------------------
Output:
Series Object
0 a
1 e
2 i
3 o
4 u
dtype: object
-----------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------
# Program No. 6
# Program to create a Series object using three different words
# 'I','am an','Indian'
import pandas as abcd
s3=abcd.Series(['I','am an','indian'])
print("Series Object")
print(s3)
-----------------------------------------------------------------------------------------------------------------------------------------------
Output:
Series Object
0 I
1 am an
2 indian
dtype: object
-----------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------

Email Id :- mirza.baig@oakridge.in  6300375851 9849104296 Page No :- 3 / 18


Unit No. I Data Handling using Pandas and Data Visualization Class Room Notes
Difference between Series and Dataframes:
Sl.no Series Dataframes

1 1-Dimensional 2- Dimensional

2 Homogeneous i.e. all the elements Heterogeneous i.e. Dataframes objects can
must be of same data type in a series have elements of different data types
object

3 Value mutable : values can change Value mutable : value can change

4 Size immutable: size of a series object Size mutable: size of a dataframe object
once created cannot be changed. If once created can change in place. you can
you want to add/drop an element, add/drop in an existing dataframe object
internally a new series object will be
created

Specify data as a Python Dictionary


The sequence that you provide with Series () can be any sequence, including dictionaries.
-----------------------------------------------------------------------------------------------------------------------------------------------
# Program No. 7
# Program to create a Series object using a dictionary
# Stores number of students in each section
import pandas as pd
stu={'12A':25, '12B':27, '12C':28, '12D':23}
s8=pd.Series(stu)
print(s8)
-----------------------------------------------------------------------------------------------------------------------------------------------
Output:
12A 25
12B 27
12C 28
12D 23
dtype: int64
-----------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------
Specify data as a scalar value:
The data can be in the form of a single value or a scalar value. But if data is a scalar value, then the index
argument to Series() function must be provided. The scalar value (given as data) will be repeated to match
the length of index.
-----------------------------------------------------------------------------------------------------------------------------------------------
# Program No. 8
# Program to create a Series that stores initial budget allocated
# Rs. 50000 for the four quarters of the year
import pandas as pd
s9=pd.Series(50000, index={'Qtr1','Qtr2','Qtr3','Qtr4'})
print(s9)
-----------------------------------------------------------------------------------------------------------------------------------------------

Email Id :- mirza.baig@oakridge.in  6300375851 9849104296 Page No :- 4 / 18


Unit No. I Data Handling using Pandas and Data Visualization Class Room Notes
Output:
Qtr1 50000
Qtr4 50000
Qtr3 50000
Qtr2 50000
dtype: int64
-----------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------
# Program No. 9
# Total number of medals to be won is 200
# Treasure fest games held every alternate year
# In the decade 2020-2029
import pandas as pd
s10=pd.Series(200, index=range(2020,2029,2))
print(s10)
-----------------------------------------------------------------------------------------------------------------------------------------------
Output:
2020 200
2022 200
2024 200
2026 200
2028 200
dtype: int64
-----------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------
Specify index(es) as well as data with Series()
While creating series types object is that along with values, you can also provide indexes. Both values and
indexes are sequences.

Syntax : <Series Object>=pandas.Series(data=None, index=None)


None: It is default values, if you skip these parameters
-----------------------------------------------------------------------------------------------------------------------------------------------
# Program No. 10
# Program to create index(es) as well as data with Series()
import pandas as pd
days=[31,28,31,30]
months=["jan","feb","Mar","Apr"]
obj3=pd.Series(index=months, data=days)
print(obj3)
-----------------------------------------------------------------------------------------------------------------------------------------------
Output:
jan 31
feb 28
Mar 31
Apr 30
dtype: int64
-----------------------------------------------------------------------------------------------------------------------------------------------

Email Id :- mirza.baig@oakridge.in  6300375851 9849104296 Page No :- 5 / 18


Unit No. I Data Handling using Pandas and Data Visualization Class Room Notes
You could skip keyword data also i.e. following statement will also do the same as above
Obj3=pd.Series(arr, index=month)
Using a Mathematical function/expression to create data array in Series()
-----------------------------------------------------------------------------------------------------------------------------------------------
# Program No. 11
# Program to create a series object that stores contribution
# amount as the value and the section names as the indexes
import pandas as pd
section =['12A','12B','12C','12D']
contribution =[5500, 7500, 9000, 3000]
s5=pd.Series(index=section, data=contribution)
print(s5)
-----------------------------------------------------------------------------------------------------------------------------------------------
Output:
12A 5500
12B 7500
12C 9000
12D 3000
dtype: int64
-----------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------
# Program No. 12
# Program to create a Series consists of
# (a) index using arange
# (b) which will double the value of index
# (c) which will calculate the square of the index
import pandas as pd
import numpy as np
a=np.arange(9,13)
print(a)
obj7=pd.Series(index=a, data=a*2)
print(obj7)
obj8=pd.Series(index=a, data=a**2)
print(obj8)
-----------------------------------------------------------------------------------------------------------------------------------------------
Output:
[ 9 10 11 12]
9 18
10 20
11 22
12 24
dtype: int32
9 81
10 100
11 121
12 144
dtype: int32
-----------------------------------------------------------------------------------------------------------------------------------------------

Email Id :- mirza.baig@oakridge.in  6300375851 9849104296 Page No :- 6 / 18


Unit No. I Data Handling using Pandas and Data Visualization Class Room Notes
-----------------------------------------------------------------------------------------------------------------------------------------------
# Program No. 13
# Program to create a series object that stores contribution
# amount as the value and the section names as the indexes
# Your school has decided to double the contribution amount
import pandas as pd
import numpy as np
section =['12A','12B','12C','12D','12E']
contribution =np.array([5500, 7500, 9000, 3000,np.NaN])
s15=pd.Series(index=section, data=contribution*2, dtype =np.float32)
print(s15)
-----------------------------------------------------------------------------------------------------------------------------------------------
Output :
12A 11000.0
12B 15000.0
12C 18000.0
12D 6000.0
12E NaN
dtype: float32
-----------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------
Important:
We can have index having duplicate entries
Python will not raise an error
Indices need not be unique in pandas Series object. This will only cause an error/if when you perform an
operation that require unique indices
-----------------------------------------------------------------------------------------------------------------------------------------------
# Program No. 14
# Program to create a Series with duplicate indexes
import pandas as pd
import numpy as np
a=np.arange(2.75,50,9.75)
print(a)
obj7=pd.Series(a, index=['a','b','a','a','b'])
print(obj7)
-----------------------------------------------------------------------------------------------------------------------------------------------
Output:
[ 2.75 12.5 22.25 32. 41.75]
a 2.75
b 12.50
a 22.25
a 32.00
b 41.75
dtype: float64
-----------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------

Email Id :- mirza.baig@oakridge.in  6300375851 9849104296 Page No :- 7 / 18


Unit No. I Data Handling using Pandas and Data Visualization Class Room Notes
Specifying/Adding NaN values in a Series Object:
Sometime you need to create a Series object of a certain size but you do not have complete data available
at that time. In such cases, you can fill missing data with a NaN (Not a number) value.

When you store a NaN value in a series object, Pandas require the data type to be of floating point type.
Even if you specify an integer type, Pandas will promote it to a floating point type (automatically) because
NaN is not supported by integer types
Series Object Attributes
When you create a Series type object, all information related to it is available through attributes. You can
use these attributes in the following formats to get information about the series object

Syntax: <Series object>.<attribute name>


Common attributes of Series Objects
Sl.No Attribute Description
1 <Series object>.index Return the Index of the series
2 <Series object>.values Return Series as ndarray or ndarray
3 <Series object>.dtype Return the dtype object
4 <Series object>.shape Return the shape
5 <Series object>.nbytes Return the number of bytes
6 <Series object>.ndim Returns the number of dimensions
7 <Series object>.size Returns the number of elements
8 <Series object>.itemsize Return the size of the dtype of the item
9 <Series object>.hasnans Return true if there are any NaN values
10 <Series object>.empty Returns true if the series object is empty
-----------------------------------------------------------------------------------------------------------------------------------------------
# Program No. 15
# Program to display the values of Series Object Attributes
import pandas as pd
import numpy as np
section=['XIIA','XIIB','XIIC','XIID']
strength=[25,np.NaN,27,28]
s1=pd.Series(index=section,data=strength)
print(s1)
print("--------------------------------------")
print("Attribute name \t\t object1")
print("--------------------------------------")
print("index :\t\t",s1.index)
print("values :\t",s1.values)
print("data type :\t",s1.dtype)
print("object shape :\t",s1.shape)
print("no.of bytes :\t",s1.nbytes)
print("ndimensions :\t",s1.ndim)
print("size :\t\t",s1.size)
print("object size :\t",s1.itemsize)
print("Has NaNs :\t",s1.hasnans)
print("empty or not :\t",s1.empty)
print("--------------------------------------")
-----------------------------------------------------------------------------------------------------------------------------------------------

Email Id :- mirza.baig@oakridge.in  6300375851 9849104296 Page No :- 8 / 18


Unit No. I Data Handling using Pandas and Data Visualization Class Room Notes
Output :
XIIA 25.0
XIIB NaN
XIIC 27.0
XIID 28.0
dtype: float64
--------------------------------------
Attribute name object1
--------------------------------------
index : Index(['XIIA', 'XIIB', 'XIIC', 'XIID'], dtype='object')
values : [25. nan 27. 28.]
data type : float64
object shape : (4,)
no.of bytes : 32
ndimensions : 1
size : 4
object size : 8
Has NaNs : True
empty or not : False
--------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------
Note:
1) The shape of a series object tell how big it is i.e. how many elements it contains including missing
or empty values(NaNs)
2) If you use len() on a Series object, then it returns total elements in it including NaNs but
<series>.count returns only the count of non NaN values in a Series object
Accessing a Series Object and its Elements:
1) Accessing Individual Elements
-----------------------------------------------------------------------------------------------------------------------------------------------
# Program No. 16
# program to Access Individual Elements
import pandas as pd
import numpy as np
month =['Feb','Jan','Apr']
days=[28,31,30]
obj5=pd.Series(index=month, data=days)
print("The whole object")
print(obj5)
print("Access individual element - Index can be given")
print("Element at Index 2")
print(obj5[2])
print("Element at Index 0")
print(obj5[0])
print("Element at Index Jan")
print(obj5['Jan'])
print("Element at Index Apr")
print(obj5['Apr'])
-----------------------------------------------------------------------------------------------------------------------------------------------

Email Id :- mirza.baig@oakridge.in  6300375851 9849104296 Page No :- 9 / 18


Unit No. I Data Handling using Pandas and Data Visualization Class Room Notes
Output:
The whole object
Feb 28
Jan 31
Apr 30
dtype: int64
Access individual element - Index can be given
Element at Index 2
30
Element at Index 0
28
Element at Index Jan
31
Element at Index Apr
30
-----------------------------------------------------------------------------------------------------------------------------------------------
Accessing Individual Elements having duplicate indexes
-----------------------------------------------------------------------------------------------------------------------------------------------
# Program No. 17
# Program to access individual element having duplicate indexes
import pandas as pd
import numpy as np
a=np.arange(2.75,50,9.75)
print(a)
obj7=pd.Series(a, index=['a','b','a','a','b'])
print(obj7)
print("To display element with duplicate indexes")
print(obj7['a'])
print("To display element with duplicate indexes")
print(obj7['b'])
-----------------------------------------------------------------------------------------------------------------------------------------------
Output:
[ 2.75 12.5 22.25 32. 41.75]
a 2.75
b 12.50
a 22.25
a 32.00
b 41.75
dtype: float64
To display element with duplicate indexes
a 2.75
a 22.25
a 32.00
dtype: float64
To display element with duplicate indexes
b 12.50
b 41.75
dtype: float64
-----------------------------------------------------------------------------------------------------------------------------------------------

Email Id :- mirza.baig@oakridge.in  6300375851 9849104296 Page No :- 10 / 18


Unit No. I Data Handling using Pandas and Data Visualization Class Room Notes
-----------------------------------------------------------------------------------------------------------------------------------------------
Extracting Slices from Series Object
-----------------------------------------------------------------------------------------------------------------------------------------------
# Program No. 18
# Program to Extract Slices from Series Object
import pandas as pd
slno=[0,1,2,3,4,5,6]
fruits=["Apple","Banana","Grapes","Jackfruit","Mango","Orange","Pineapple"]
k=pd.Series(index=slno, data=fruits)
print(k)
print('k[1:] value :')
print(k[1:])
print('k[2:5] value :')
print(k[2:5])
print('k[0: :2] value :')
print(k[0: :2])
print('k[-3] value :')
print(k[-3:])
print('k[:-3] value :')
print(k[:-3])
-----------------------------------------------------------------------------------------------------------------------------------------------
Output:
0 Apple
1 Banana
2 Grapes
3 Jackfruit
4 Mango
5 Orange
6 Pineapple
dtype: object
k[1:] value :
1 Banana
2 Grapes
3 Jackfruit
4 Mango
5 Orange
6 Pineapple
dtype: object
k[2:5] value :
2 Grapes
3 Jackfruit
4 Mango
dtype: object
k[0: :2] value :
0 Apple
2 Grapes
4 Mango
6 Pineapple
dtype: object
k[-3] value :
4 Mango
5 Orange
6 Pineapple
dtype: object
k[:-3] value :

Email Id :- mirza.baig@oakridge.in  6300375851 9849104296 Page No :- 11 / 18


Unit No. I Data Handling using Pandas and Data Visualization Class Room Notes
0 Apple
1 Banana
2 Grapes
3 Jackfruit
dtype: object
-----------------------------------------------------------------------------------------------------------------------------------------------
Consider a Series Object p that stores the number of students in each section of class12. First two sections
have been given the task of selling movie ticket @100/- per ticket as part of SEWA project. Write the code to
display how much they have collected.
-----------------------------------------------------------------------------------------------------------------------------------------------
# Program No. 19
# Program to create a Series object p that stores the number
# of students in each section of class12
# two section have been given the task of selling movie tickets
# calculate the amount collected for each section
# if the cost of movie ticket is Rs 100/-
import pandas as pd
sec=['12A','12B','12C','12D','12E']
strength=[20,25,30,25,30]
p=pd.Series(index=sec, data=strength)
print(p)
print(p[ :2]*100)
print(p[ :5:2]*100)
print(p[ : :2]*100)
-----------------------------------------------------------------------------------------------------------------------------------------------
Output:
12A 20
12B 25
12C 30
12D 25
12E 30
dtype: int64
12A 2000
12B 2500
dtype: int64
12A 2000
12C 3000
12E 3000
dtype: int64
12A 2000
12C 3000
12E 3000
dtype: int64
-----------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------
Operations on Series Object
1) Modifying Elements of Series Object
Syntax:
<SeriesObject>[<index> ]= <new_data_value>
<SeriesObject>[<start:stop> = <new_data_value>

Email Id :- mirza.baig@oakridge.in  6300375851 9849104296 Page No :- 12 / 18


Unit No. I Data Handling using Pandas and Data Visualization Class Room Notes
-----------------------------------------------------------------------------------------------------------------------------------------------
# Program No. 20
# Program to modify elements of Series Object using index
import pandas as pd
sec=['12A','12B','12C','12D','12E']
strength=[20,25,30,25,30]
p=pd.Series(index=sec, data=strength)
print(p)
print("Modifying elements of Series Object using index")
p[0]=40
print(p)
print("Modifying elements of Series Object using start and stop")
p[2:4]=50
print(p)
print("Modifying elements of Series Object using just start value")
p[2:]=100
print(p)
-----------------------------------------------------------------------------------------------------------------------------------------------
Output:
12A 20
12B 25
12C 30
12D 25
12E 30
dtype: int64
Modifying elements of Series Object using index
12A 40
12B 25
12C 30
12D 25
12E 30
dtype: int64
Modifying elements of Series Object using start and stop
12A 40
12B 25
12C 50
12D 50
12E 30
dtype: int64
Modifying elements of Series Object using just start value
12A 40
12B 25
12C 100
12D 100
12E 100
dtype: int64
-----------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------

Email Id :- mirza.baig@oakridge.in  6300375851 9849104296 Page No :- 13 / 18


Unit No. I Data Handling using Pandas and Data Visualization Class Room Notes
2) Renaming Indexes
<Object>.index=<new index array>
-----------------------------------------------------------------------------------------------------------------------------------------------
# Program No. 21
# Program to rename index
import pandas as pd
sec=['12A','12B','12C','12D','12E']
strength=[20,25,30,25,30]
p=pd.Series(index=sec, data=strength)
print(p)
p.index=['a','b','c','d','e']
print(p)
-----------------------------------------------------------------------------------------------------------------------------------------------
Output:
12A 20
12B 25
12C 30
12D 25
12E 30
dtype: int64
a 20
b 25
c 30
d 25
e 30
dtype: int64
-----------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------
The head() and tail() function
The head() function is used to fetch first n rows from a panda object ad tail() function returns last n rows
from a panda object. The syntax to use this functions is:
Syntax:
<Pandas object>.head([n])
<Pandas object>.tail([n])
-----------------------------------------------------------------------------------------------------------------------------------------------
# Program No. 22
# Program to demonstrate head and tail functions
import pandas as abcd
s3=abcd.Series(['a','b','c','d','e','f','g','h'])
print('head(5)')
print(s3.head(5))
print('tail(5)')
print(s3.tail(5))
print('head(2)')
print(s3.head(2))
-----------------------------------------------------------------------------------------------------------------------------------------------

Email Id :- mirza.baig@oakridge.in  6300375851 9849104296 Page No :- 14 / 18


Unit No. I Data Handling using Pandas and Data Visualization Class Room Notes
Output:
head(5)
0 a
1 b
2 c
3 d
4 e
dtype: object
tail(5)
3 d
4 e
5 f
6 g
7 h
dtype: object
head(2)
0 a
1 b
dtype: object
-----------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------
Vector Operations on Series Object:
Vector operations means that if you apply a function or expression then it is individually applied on each item
of the object. Since series objects are built upon NumPy arrays (ndarrays), they also support vectorised
operations, just like ndarrays.
-----------------------------------------------------------------------------------------------------------------------------------------------
# Program No. 23
# Program to demonstrate Vector Operations on Series Object
import pandas as pd
import numpy as np
np=[1.50,12.75,24.00,35.25,46.50]
k=pd.Series(data=np)
print(k)
print(k+2)
print(k*3)
print(k**2)
print(k>25)
-----------------------------------------------------------------------------------------------------------------------------------------------
Output:
0 1.50
1 12.75
2 24.00
3 35.25
4 46.50
dtype: float64
0 3.50
1 14.75
2 26.00
3 37.25

Email Id :- mirza.baig@oakridge.in  6300375851 9849104296 Page No :- 15 / 18


Unit No. I Data Handling using Pandas and Data Visualization Class Room Notes
4 48.50
dtype: float64
0 4.50
1 38.25
2 72.00
3 105.75
4 139.50
dtype: float64
0 2.2500
1 162.5625
2 576.0000
3 1242.5625
4 2162.2500
dtype: float64
0 False
1 False
2 False
3 True
4 True
dtype: bool
-----------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------
# Program No. 24
# Program to add values which are there in two different Series3
# Program to add students of grade11 and grade12 Stream wise
import pandas as pd
g11=pd.Series(data=[30,40,50], index=['Science','Commerce','Humanities'])
g12=pd.Series(data=[25,45,55], index=['Science','Commerce','Humanities'])
print("Total No. of Students Stream wise")
print(g11+g12)
-----------------------------------------------------------------------------------------------------------------------------------------------
Output:
Total No. of Students Stream wise
Science 55
Commerce 85
Humanities 105
dtype: int64
-----------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------

Email Id :- mirza.baig@oakridge.in  6300375851 9849104296 Page No :- 16 / 18


Unit No. I Data Handling using Pandas and Data Visualization Class Room Notes
# Program No. 25
# Predict the output of the following program
import pandas as pd
info=pd.Series(data=[31,41,51])
print(info)
print(info>40)
print(info[info>40])
-----------------------------------------------------------------------------------------------------------------------------------------------
Output:
0 31
1 41
2 51
dtype: int64
0 False
1 True
2 True
dtype: bool
1 41
2 51
dtype: int64
-----------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------
Sorting Series Values: You can sort the values of a Series object on the basis of values and indexes
(i) Sorting on the Basis of Values : To Sort a Series Object on the basis of values, you may use
sort_values() function as per the following index:
Syntax : <Series Object>.sort_values([ascending=True/False])
Optional argument = True
info.sort_values()
0 31
1 41
2 51
dtype: int64
info.sort_values(ascending=False)
2 51
1 41
0 31
dtype: int64

info.sort_values (ascending=True)
0 31
1 41
2 51
dtype: int64
-----------------------------------------------------------------------------------------------------------------------------------------------

Email Id :- mirza.baig@oakridge.in  6300375851 9849104296 Page No :- 17 / 18


Unit No. I Data Handling using Pandas and Data Visualization Class Room Notes
(ii) Sorting on the Basis of Indexes
To sort a Series Object on the bases of indexes, you may use sort_index () function as per the following
syntax
Syntax: <Series.object>.sort_index ([ascending=True/False])
info.sort_index (ascending=False)
info.sort_index ( ) / info.sort_index (ascending=True)
-----------------------------------------------------------------------------------------------------------------------------------------------
# Program No. 26
# Program to sort Series Values based on Values and Indexes
import pandas as pd
sec=['12A','12B','12C','12D','12E']
strength=[20,25,30,15,30]
p=pd.Series(index=sec, data=strength)
print("Sorting values Ascending \n",p.sort_values())
print("Sorting values Descending \n",p.sort_values(ascending=False))
print("Sorting index Descending \n",p.sort_index(ascending=False))
print("Sorting index Ascending \n",p.sort_index(ascending=True))
-----------------------------------------------------------------------------------------------------------------------------------------------
Output:
Sorting values Ascending
12D 15
12A 20
12B 25
12C 30
12E 30
dtype: int64
Sorting values Descending
12E 30
12C 30
12B 25
12A 20
12D 15
dtype: int64
Sorting index Descending
12E 30
12D 15
12C 30
12B 25
12A 20
dtype: int64
Sorting index Ascending
12A 20
12B 25
12C 30
12D 15
12E 30
dtype: int64
-----------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------

Email Id :- mirza.baig@oakridge.in  6300375851 9849104296 Page No :- 18 / 18

You might also like