CLASS - XII: INFORMATICS PRACTICES (2024-25)
QUICK REVISION
UNIT-I [25 Marks] 1M 2M 3M 4M 5M Total
Data Handling using Pandas and Data Visualization 9 4 3 4 5 25 M
Introduction to Python Libraries-
A Python library or package is a collection of Python modules containing ready-to-use functions to perform
specific task related to an application. The advantage of using libraries is that we can import libraries and can use
its different functions in Python program. Some commonly used Python Libraries are-
1. Python Standard Library: It is a collection of modules which is available after installing Python. Examples of
such libraries are – (a) math module- provides mathematical functions (b) random module- provides functions
for generating random numbers. (c) statistics module- provides statistical functions
2. Numpy (Numerical Python) library: Offers functions for working with matrices and multi-dimensional arrays.
3. Pandas (PANel + DAta) library: Pandas is a fast, powerful, flexible and easy to use open-source library for data
manipulation and analysis of data. Commonly used to handle Series and Data Frame.
4. Matplotlib library: It provides functions for Visualization of Data i.e. plotting and drawing graphs.
A Python library can be imported in a program by using import command as – Ex: import pandas as pd
import <library Name> [as <object>] import numpy as np
What is PANDAS? Features of Pandas:
Pandas (PANel DAta System) is fast and Pandas Data structures are Fast and efficient in functionality.
powerful open source data analysis library of Supports functions to handle missing data.
Python developed by Wes McKinney. Pandas Supports Label-based slicing and indexing.
has functions for analysing, cleaning, Offers conditional selection, merging and joining of data sets.
exploring, and manipulating massive data. Offers import/export to handle variety of data source.
Data Structure in PANDAS:
A data structure is a collection of data values and operations that can be applied to that data. It enables efficient
storage, retrieval and modification of data.
Pandas offer the Series and Data Frame data structures for handling and analysis of big data.
Series: DataFame:
Series is one dimensional (1D) homogeneous data Data Frame is two-dimensional (2D) heterogenous
structure with labelled index. data structure with rows and columns like Excel sheet.
Series is size immutable and Value Mutable i.e. Data Frame is both Size and Value mutable i.e. you can
new values cannot be added but existing values add new rows /columns or delete.
can be deleted and modified. Rows and Column of Data Frame are indexed or
The default positional index starts from zero, if labelled with row index and column index. Default
index/label are not given. positional index starts with 0, if indexes are not given.
Creating Series:
A series can be created using Series() function with
optional parameters for data and index.
<Series Object>= [Link]([data=<data set>],
[index=<index set>], [dtype=<datatype>])
Where dataset can be a list/Dictionary or any scalar
value.
Series DataFrame
Creating Empty Series: import pandas as pd Output:
An empty series can be created using Series () s= [Link]() Series([], dtype:
without any parameters. print(s) float64)
Creating Series with ndarray: import pandas as pd 0 10
A series can be created using numPy array (ndArray), import numpy as np 1 20
as data in Series() method. Default index [0,1,2..] will arr=[Link]([10,20,30,40]) 2 30
be generated, if labeled index is not given. s= [Link](arr) 3 40
print(s)
Page: 1 of 25
Creating Series with list: import pandas as pd p 5
A series can be created by passing a list as data lst=[5,6,7,8] q 6
in Series() method. idx=[‘p’,’q’,’r’,’s’] r 7
If no index is given then by default index will s=[Link](lst,index=idx) s 8
be generated as *0,1,2,3…len(list)-1]. print(s)
Creating Series with Dictionary: import pandas as pd A 45
A series can be created using dictionary as data d={‘A’:45,’B’:67,’C’:34,’D’:78} B 67
in Series() method. s= [Link](d) C 34
The keys of dictionary become index and values print(s) D 78
are assumed as data. import pandas as pd A 45.0
Note: If index is provided then Series created d={‘A’:45,’B’:67,’C’:34,’D’:78} S NaN
with matching index and Keys only. NaN (Not a idx=[‘A’,’S’,’D’,’C’] D 78.0
number) value is used for not matching index. s= [Link](d,index=idx) C 34.0
print(s)
Creating Series with Scalar value: import pandas as pd 0 8
A series can be created using a constant (Scalar) s1=[Link](8) ----------
value. The value is repeated as per given index. print(s1) a 5
s2=[Link](5,index=[‘a’,’b’,’c’]) b 5
print(s2) c 5
Accessing Elements of Series: import pandas as pd
Data values of a series can be accessed in three ways- val=[10,20,30,40,50,60] 20
Positional Index: Single value at given index. idx=['a','b','c','d','e',’f’] ---------
s=[Link](val,index=idx) 20
Labelled Index: Single value at given index.
# using positional index ---------
Slicing: To assess multiple values (subset) for given
print (s[1]) b 20
range of indexes.
# using labelled index c 30
The range can be defined as [start : end : step]. Default
print(s[‘b’]) d 40
step value is 1. Negative step value will cause access of
e 50
series in reverse order. # slicing using Positional index
Note: print (s[1:5]) b 20
Labeled index : values are retrieved from START to END # Slicing using labelled index c 30
Positional index: values are retrieved from START to END-1 print(s[‘b’:’d’]) d 40
Conditional Access of Series elements: import pandas as pd a False
You can filter/access data values of a series based on val=[5,20,10,80,25] b False
defined condition. c False
idx=['a','b','c','d','e']
Applying condition on whole Series: d True
It will return True or False based on given condition. s=[Link](val,index=idx) e True
Applying condition on elements: # applying condition on whole series -------------
It will returns selected values based on condition. print(s>20) d 80
Any single conditional expression with Relational # applying condition on elements e 25
operators (>,<,=,!=,>=,<=) can be applied on series. print ( s[s>20] )
Modifying Series Elements : import pandas as pd a 100
You can modify data value of lst=[10,20,30,40,50] b 20
corresponding index by providing index/ c 5
position. To modify values in a range of idx=[‘a’,’b’,’c’,’d’,‘e’])
d 5
indexes, slicing can be used. s=[Link](lst,index =idx) e 5
# modifyng single value p 100
Series[index] = <new value> s[‘a’]=100 q 20
Series[start : end] = <new value> # modifying multiple values r 5
s 5
Modifying Index of Series: s[‘c’:’e’]=5
t 5
Index of Series can be modified using index print(s)
attribute of series. #modifying index
[Link] = <new index values> [Link]=[‘p’,’q’,’r’,’s’,’t’]
print(s)
Page: 2 of 25
Series Attributes: import pandas as pd a 4.0
Certain properties of a Series can be accessed using import numpy as np b 5.0
its attributes as [Link]. lst=[4, 5, [Link], 7, 8, 9] c NaN
Attribute Purpose idx=['a', 'b', 'c', 'd', 'e', 'f']d 7.0
name Used to assign name to Series. s= [Link](lst, index=idx) e 8.0
Size Returns size (number of elements) of [Link]="MySeries" f 9.0
series print(s) Name: MySeries, dtype:
Shape Returns shape of series as tuple print([Link]) float64
print([Link]) 6
values Returns data values as ndarray
print([Link]) (6,)
index Returns index labels
print([Link]) True
Dtype Returns data type of seiries print([Link])
Empty Returns true is series is empty float64
print([Link]) False
otherwise false print([Link])
Hasnans Returns true if series has NaN values. [ 4. 5. nan 7. 8. 9.]
Index(*'a‘,'b‘,'c‘,'d‘,'e‘,'f'],
dtype='object')
Series Methods: import pandas as pd 6
Series methods specific operations on Series (s). lst=[4,5,6,7,8,9] a 4
head( ) Returns top 5 values of series, if no value is given. idx=['a','b','c','d','e','f'] b 5
tail( ) Returns bottom 5 values of series, if no value is given. s= [Link](lst, index=idx) c 6
count( ) Returns counting of not-NaN values in series. print([Link]()) -------------
The following Mathematical methods are applicable on numeric print([Link](3)) e 8
series only. print([Link](2)) f 9
print([Link]()) -------------
min( ) Returns minimum value of the series. print([Link]()) 4
max( ) Returns maximum value of the series. print([Link]()) 9
sum( ) Returns total of value of the series. print([Link](5)) 39
add( ) Adds a scalar (constant) value or another series.
a 9
sub( ) Subtracts a scalar (constant) value or another series.
b 10
mul( ) Multiplies series with scalar (constant) values or c 11
another series. d 12
div( ) Devides series with scalar (constant) values or another e 13
series. f 14
Mathematical operations on Series: import pandas as pd a 9
Mathematical operations on series can be applied in lst=[4,5,6,7,8] b 10
two ways. idx=['a','b','c','d','e'] c 11
Using operators : +, - ,*, / , //, % etc. s= [Link](lst, index=idx) d 12
Using Methods: add(), sub(), mul(), div() etc. #applying arithmetic operation e 13
Mathematical operations can be two types- print([Link](5)) # OR print(s+5)
Manipulating Series with scalar (constant) import pandas as pd a 14.0
value: s1= [Link]([4,5,6,7], b 20.0
When a series is manipulated with a constant index=['a','b','c','d']) c 26.0
number then mathematical operation is applied with s2= [Link]([10,12,15,18,20], d NaN
each element of the series (Vector arithmetic). index=['a','p','b','q','c']) p NaN
Manipulating two Series: #applying arithmetic operation q NaN
When mathematical operation is applied on two print([Link](s2)) #OR (s1+s2)
series then operation is performed on matching #applying arithmetic operation a 14.0
index. The NaN value will be produced for non- # 0 will be assumed for non-matching b 20.0
matching/missing value. values # before adding series. c 26.0
d 7.0
Note: We can use fill_value parameter to avoid NaN [Link](s2, fill_value=0) p 12.0
result for non-matching values.
q 18.0
Page: 3 of 25
Data Frame:
A Data Frame is 2-dimensional heterogeneous data structure arranged in
tabular form containing rows and columns. Each row and column may
have labeled (Text) index as well as Positional (Numeric) Index.
The default numeric index labels starts from zero, if no index is given.
Dimensions of DataFrame are also called Axis. DataFrame is Size and
Data mutable.
Creating Data Frame: A Data Frame in Pandas can be created
Data Frame can be created in the following two main approaches. using DataFrame() function with optional
(Row wise) List-List [ [R1],[R2],[R3],..] parameters for data , row index and
List with nested List-Dictionary [ {..},{..},{..},..] column index.
structure. List-Series [S1,S2,S3,..] <DF Object>= [Link](<2D-
(Column wise) Dictionary-List { [C1],[C2],[C3],..} data set>, index=[<Row indexes>],
Dictionary with Dictionary -Dictionary { {..},{..},{..},..} columns=[<column indexes>])
nested structure. Dictionary -Series { S1,S2,S3,..} Where 2D-dataset may be ndarray or
Data Frame can also be created using ndarray or another Data Frame. nested structure of list of dictionary.
Creating Empty DataFrame: Creating Data Frame using List of Lists (Nested List):
import pandas as pd import pandas as pd
df= [Link]() lst=[['Amar',60,68,45], ['Akbar',65,65,56], ['Anthony',70,77,65]]
print(df) df= [Link](lst,index=[1,2,3], columns=['Name','Phy','Chem','Maths'])
print(df)
Creating Data Frame with list of Dictionary:
import pandas as pd
lst= [ {'Name':'Amar','Phy':60,'Chem':68,'Maths':45}, {'Name':'Akbar','Phy':65,'Chem':65,'Maths':56},
{'Name':'Anthony','Phy':70,'Chem':77,'Maths':65} ]
df= [Link](lst, index=[1,2,3])
print(df)
Creating Data Frame with Dictionary of Series
s1=[Link](['Amar','Akbar','Anthony'],index=[1,2,3])
s2=[Link]([60,65,70], index=[1,2,3] )
s3=[Link]([68,65,77], index=[1,2,3] )
s4=[Link]([45,56,65], index=[1,2,3] )
dct={'Name':s1,'Phy':s2,'Chem':s3,'Maths':s4} Note: Keys of dictionary become column index
df= [Link](dct) and values arranged as per matching keys)
print(df)
Accessing Data Frame:
The values of data frame can be accessed in different ways like
Series. The possible ways are-
Accessing column(s)
Accessing Row(s)
Accessing Row(s) and column(s)
Accessing Columns of DataFrame: print(df[‘Phy’]) OR
Single column: print([Link])
<DataFrame>[<Col Label>] OR <DataFrame>.<Col Label> print(df[[‘Phy’, ‘Maths’]])
Multiple Columns: <DataFrame>[ [Col 1, Col2, Col3..] ] Print(df[[‘Maths’, ‘Phy’]])
Accessing Rows of Data Frame: # from row one onward
Rows can be accessed by giving Index range. print(df[‘one’: ])
<DataFrame>[<Start row label>:<End row label>] print(df[ :‘three’]) # till three row
When start or end label is not provided, default value fist # from row one to three
row and last row will be used. print(df[‘one’: ‘three’])
When labeled index used the START to END will be # Single Row ‘Two’ only
displayed. In case of Positional index (number) then Print(df[‘two’:‘two’])
START to END-1 will be displayed. Print(df[1:3]) # using positional index
Pandas head() and tail() function can also be used. Print([Link](2))
Page: 4 of 25
Accessing Rows and Columns using Label index: Accessing Rows and columns using Positional
Pandas loc[] is provides subset of selected rows, columns Indexes.
or both using row/column labels. It returns selected iloc[] method to get subset of selected combinations
subset from given Row/Col label from Start to End. of rows, columns or both using positional indexes.
<DF object>.loc[<start row>:<end row>, <start <DF object>.iloc[<start row>:<end row>, <start
column>:<end column>] column>:<end column>]
When start/end row/column is not provided, default Note: iloc() used positional indexes and row/col will
value first row/column and last row/column will be used. be selected from START to END-1.
# all rows and all columns # all rows and all columns
print([Link][ : , : ]) print([Link][ : , : ])
# from row ‘one’ to end row and all columns # from row ‘one’ to end row and all columns
print([Link]['one': , : ]) print([Link][0: , : ])
# till row ‘three’ and all columns # till row ‘two’ and all columns
print([Link][ :'three', : ]) print([Link][ :2 , : ])
# all rows and from ‘Phy’ column to end # all rows and from ‘Phy’ column to end
print([Link][ : ,'Phy': ]) print([Link][ : ,1: ])
# all rows and from ‘phy’ to ‘Maths’ column # all rows and from ‘phy’ to ‘Chem’ column
print([Link][ : ,'Phy':'Maths']) print([Link][ : ,1:3])
# from Row ‘Two’ to ‘three’ and ‘Phy’ to ‘Chem’ col #from Row ‘two’ to ‘two’ and ‘Phy’ to ‘Chem’
print([Link]['two':'three','Phy':'Chem']) print([Link][1:2,1:3])
Accessing Individual Value: # accessing value of individual cell
There are three methods to access individual value. print([Link]['three'])
<DFobject>.<Column>[<row label or row position>] print([Link]['three'])
<DFobject>.at[<row label>, <column label>] print([Link][2])
<DFobject>.iat[<row position>, <column position>] print([Link]['two','Chem'])
print([Link][1,1])
Accessing Items using condition # applying condition on „Phy‟ Column
You can also filter and analyze data by displaying print(df['Phy']>60)
True/False based on given condition. # filtering data on condition
The condition may have relational operators like print(df[ df['Phy']>60 ])
>,>=,<,<=,!=,= etc. # applying condition on range
print([Link]['one':'two','Phy': ]>60)
print([Link][df[‘Chem’]>60,[‘CS’])
print([Link][df[‘Chem’]>60,[‘CS’,’Maths’])
Accessing Items based on Boolean Indexing: dct={'Name':['Amar','Akbar','Anthony','Manpreet'],
Boolean indexing as name suggests, having 'Phy':[60,65,70,67], 'Chem':[34,55,32,46],
Boolean values (True/False or 1/0) as row 'Maths':[45,56,65,75]}
index/label to access the rows based on True or df=[Link](dct, index=[True,False,True,False])
False criteria. For this we have to create/change print(df)
row labels as True or False. # accessing rows based on Boolean value of rows.
print([Link][True])
print([Link][False])
Modifying Data Frame: #modifying value of existing column ‘Maths’
Modifying values of Existing Column: df[‘Maths’]=50
Single Column: #modifying value of ‘Phy’ to ‘Chem’ column
<DF object>[<Column Label>]=<new value(s)> [Link][:,’Phy’: ‘Chem’]= 45
Multiple Column #Adding new column ‘Eng’ with 65 marks
<DF Object>.loc[: , Column 1: column 2]=<new value(s)> df[‘Eng’]=65
If given column already exists in the Data Frame then existing # Adding new column ‘IP’ with new marks
values will be changed otherwise a new column will be added in df[‘IP’]=[45,54,67]
the Data Frame.
Adding New Column: df[‘IP’]=[45,54,67]
<DF object>[<Column Label>]=<new value(s)> df[‘Total’]=
We can also use expressions while creating new columns. df[‘Phy’]+df[‘Chem’]+df[‘Maths’]
Adding new Row: # Adding new row ‘five’ with different marks
<DF Object>.loc[<Row index> ]=<new value(s)> [Link][‘five’]=[‘Manpreet’,45,54,67]
Page: 5 of 25
Modifying values of Rows: [Link][‘one’]=50
<DF Object>.loc[<Row 1> : <Row 2> ]=<new value(s)> #modifying value of ‘two’ to ‘three’ row
Rows of Data Frame can be modified by selecting and assigning [Link][‘two’:’three’,:]=60
new values. If given row already exists then existing values will #Adding new row ‘four’ with 65 marks
be changed otherwise a new row will be added. [Link][‘four’,:]=65
Modifying single value at specified position: [Link]['two']=10
<DFobject>.<Column>[<row label or position>]=<new value> [Link][0]=20
<DFobject>.at[<row label>, <column label>]=<new value> [Link]['two',‘Phy’]=30
[Link][2,1]=40
Deleting Rows and Columns of Data Frame: # Deleting a column ‘Maths’
Rows or column of DataFrame can be deleted by two methods- del df['Maths']
del <DF object>[<column label>] # to delete a single column # Deleting a row ‘two’
df=[Link](['two'])
<DF Object>.drop([<Row labels/Column labels>], axis=0/1)
#deleting multiple column
Where axis=1 for column deletion and axis=0 for row deletion. df=[Link](['Phy','Chem'], axis=1)
Using drop() method, you have to assign modified Data Frame on # deleting multiple rows
same or different object, since drop() deletes on copied object. df=[Link](['one','four'], axis=0)
Renaming row and column labels: # Renaming Row labels
Row and column indexes can be rename as- df=[Link](index={'P':'one','Q':'two','R':'three','S':'four'})
<DF Object>.rename( index={name-dict}, # Renaming column labels
columns={column-dict} ) df=[Link](columns={'A':'Name','B':'Phy','C':'Chem',
'D':'Maths'} )
Data Frame Attributes:
Once Data Frame has been created, you can access certain properties of Data Frame through predefined
attributes.
You can access series attributes as <DF>.<attribute>
Attributes Purpose
index Returns list of row indexes /labels of Data Frame.
columns Returns list of column indexes/labels of Data Frame.
values Returns data values of Data Frame as ndarray.
size Returns the size (number of data items) of DataFrame.
shape Returns shape (dimensionality) of Data Frame as tuple
axes Returns list of both axes (row and columns labels)
dtype Returns column wise data type of Data Frame.
empty Returns true if Data Frame is empty otherwise false.
T Transpose index and columns.
import pandas as pd
dct={'Phy':[60,65,70], 'Chem':[34,55,32], 'Maths':[45,56,65]}
df=[Link](dct, index=['Amar','Akbar','Anthony'])
print(df)
print([Link])
print([Link])
print([Link])
print([Link])
print([Link])
print([Link])
print([Link])
print(df.T)
Data Frame Methods:
Apart from mathematical methods add(), sub(), mul() , div() etc., Pandas offers other methods too, which can be
used for different operations on the Data Frame.
Page: 6 of 25
Methods Purpose print([Link]()) #OR
head( ) Returns top 5 rows of Data Frame, if no value is given. print([Link](axis=0))
tail( ) Returns bottom 5 rows, if no value is given. print([Link](axis=1))
len(DF) Return numbers of rows in the Data Frame. print(df['Phy'].count())
The following functions can be applied in Row-wise/Column wise by print([Link]()) #OR
specifying axis or whole data frame.(axis=1 for Row-wise and axis=0 for print([Link](axis=0))
column wise). If axis is not given then 0 will be assumed. print([Link](axis=1))
print(df['Phy'].sum())
count() Returns counting of not-NaN values in Data Frame.
print([Link]()) #OR
min( ) Returns minimum value of the DataFrame/Row/column.
print([Link](axis=0))
max( ) Returns max value of the Data Frame/ Rows/ columns. print([Link](axis=1))
sum( ) Returns total of value of the Data Frame/Rows/Columns. print(df['Phy'].max())
Mathematical Operations on DataFrame:
Pandas offers add(), sub(), mul(), div() functions to manipulate a data frame with scalar values or another data
frame. Arithmetic operators (+,-,*,/,//,% etc) can also be used.
Manipulating DataFrame with scalar (Vector Operation): All elements will be manipulated with values.
import pandas as pd
dct={'Name':['Amar','Akbar','Anthony','Manpreet'],
'Phy':[60,65,70,67], 'Chem':[34,55,32,46],
'Maths':[45,56,65,75]}
df=[Link](dct,index=['One','Two','Three','Four'])
print(df)
#Mathematical Operation on a column
df['Chem']=df['Chem']-5
df['Maths']=df['Maths'].add(5)
print(df)
Importing & Exporting Data Frame:
The data of Data Frame can be saved as a file on storage media, so that it can be utilized by other applications.
The Data Frame can be saved in different formats but CSV type is simple and commonly used.
The Comma Separated Value (CSV) is a simple plain text format which can be created in any Text Editor (Note
Pad), Spreadsheet (MS Excel) etc., where all values are separated by comma (,).
Pandas offers the following two methods to import and export data from/to CSV file.
read_csv(): reads/imports data from CSV file to Data Frame.
to_csv(): Creates/exports data of Data Frame to CSV file.
Importing (Reading) data from CSV file: Example:
<DF>=pandas.read_csv(<“path-filename”>, sep=<“Separator ”>, import pandas as pd
skiprows=<n>, nrows=<n> ) df=pd.read_csv("e:\\[Link]")
<“path-filename”>: specifies the filename with path. Path name must be print(df)
separated by \\. Ex: “c:\\Data\\[Link]”
sep=<“Separator ”>: specifies the delimiter character. Default is comma.
skiprows=<n>: specifies the number of lines to be skipped while reading.
nrows=<n>: specifies the number of lines to be read from CSV file.
Exporting (Writing) Data to CSV file: import pandas as pd
<DF>.to_csv(<“path-filename”>, sep=<“Sep char”> ) dct={'Name':['Amar','Akbar','Anthony','Manpreet'],
<“path-filename”>: specifies the filename with path Phy':[60,65,70,67],'Chem':[34,55,32,46],
(location) to be created. Ex: “c:\\Data\\[Link]” 'Maths':[45,56,65,75]}
sep=<“Separator char”>: specifies the delimiter character df=[Link](dct, index=[1,2,3,4])
to separate data values. Default is comma(,). print(df)
Row labels & column labels are also written in CSV file. df.to_csv("e:\\[Link]")
Page: 7 of 25