0% found this document useful (0 votes)
2 views24 pages

Module 5 - Data Handlinng Using Pandas-Inotes

The document provides an overview of the Pandas library, which is essential for data analysis and manipulation, highlighting its key data structures: Series and DataFrame. It explains how to create and manipulate these structures, including operations like indexing, slicing, and mathematical operations. Additionally, it covers the properties of DataFrames and methods for adding, renaming, and deleting columns.

Uploaded by

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

Module 5 - Data Handlinng Using Pandas-Inotes

The document provides an overview of the Pandas library, which is essential for data analysis and manipulation, highlighting its key data structures: Series and DataFrame. It explains how to create and manipulate these structures, including operations like indexing, slicing, and mathematical operations. Additionally, it covers the properties of DataFrames and methods for adding, renaming, and deleting columns.

Uploaded by

pattankirti5m
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Data Handling using Pandas –I

Pandas:
• It is a package useful for data analysis and manipulation.
• Pandas provide an easy way to create, manipulate and wrangle the
data.
• Pandas provide powerful and easy-to-use data structures, as well
as the means to quickly perform operations on these structures.

Data scientists use Pandas for the following advantages:

• Easily handles missing data.


• It uses Series for one-dimensional data structure and DataFrame
for multi-dimensional data structure.
• It provides an efficient way to slice the data.
• It provides a flexible way to merge, concatenate or reshape the
data.

DATA STRUCTURE IN
PANDAS
A data structure is a way to arrange the data in such a way that it can
be accessed quickly, and we can perform various operations on this data
like- retrieval, deletion, modification, etc.

Pandas deal with 3 data sstructures

1. Series
2. Data Frame
3. Panel

We are having only a series and data frames in our syllabus.

Page 1 of 24
Series
Series-Series is a one-dimensional array-like structure with
homogeneous data, which can be used to handle and manipulate data.
What makes it special is its index attribute, which has incredible
functionality and is heavily mutable.

It has two parts-


1. Data part (An array of actual data)
2. Associated index with data (associated array of indexes or data labels)

e.g.-

Index Data

0 10

1 15

2 18

3 22

 We can say that a Series is a labeled one-dimensional array that can


hold any type of data.
 Data of Series is always mutable, which means it can be changed.
 But the size of the Data of the Series is always immutable, which
means it cannot be changed.
 Series may be considered as a Data Structure with two arrays out
of which one array works as Index (Labels) and the second array
works as original Data.
 Row Labels in the Series are called Index.
Syntax to create a Series:

<Series Object>=[Link] (data, index=idx (optional))

 Where data may be python sequence (Lists), ndarray,scalar value


or a python dictionary.

How to create Series with nd array

Program-

import pandas as pd
Output-
import numpy as np Default Index
0 10
arr=[Link]([10,15,18,22])
1 15
s = [Link](arr) 2 18

print(s) 3 22

Data

Here we create an
array of 4 values.
How to create Series with Mutable index

Program-

import pandas as pd Output-


import numpy as np first a
arr=[Link](['a','b','c','d']) second b
s=[Link](arr,index=['first','second','th third c
ird','fourth']) fourth d

print(s)
Creating a series from Scalar value

To create a series from scalar value, an index must be provided. The


scalar value will be repeated as per the length of index.

Creating a series from a Dictionary


Mathematical Operations in Series

Print all the values of the Series by multiplying them by 2.

Print Square of all the values of the series.

Print all the values of the Series that are greater than 2.
Head and Tail Functions in Series

head (): It is used to access the first 5 rows of a series. Note :To
access first 3 rows we can call series_name.head(3)

Result of [Link]()

Result of [Link](3)
tail(): It is used to access the last 5 rows of a series. Note :To
access last 4 rows we can call series_name.tail (4)
Selection in Series

Series provides index label loc and ilocand [] to access rows andcolumns.
1. loc index label :-

Syntax:-series_name.loc[StartRange: StopRange]
Example-

To Print Values from Index 0 to 2

To Print Values from Index 3 to 4


2. Selection Using iloc index label :-

Syntax:-series_name.iloc[StartRange : StopRange]
Example-

To Print Values from Index 0 to 1.


3. Selection Using [] :

Syntax:-series_name[StartRange> : StopRange] or
series_name[ index]
Example-

To Print Values at Index 3.


Indexing in Series

Pandas provide index attribute to get or set the index of entries or


values in series.
Example-
Slicing in Series

Slicing is a way to retrieve subsets of data from a pandas object. A slice object
syntax is –

SERIES_NAME [start:end: step]


The segments start representing the first item, end representing the last item,
and step representing the increment between each item that you would like.
Example :-

CREATED BY: SACHIN BHARDWAJ PGT(CS) KV NO1 TEZPUR, VINOD VERMA PGT (CS) KV OEF KANPUR
DATAFRAME
DATAFRAME-It is a two-dimensional object that is useful in representing
data in the form of rows and columns. It is similar to a spreadsheet or an
SQL table. This is the most commonly used pandas object. Once we store the
data into the Dataframe, we can perform various operations that are useful
in analyzing and understanding the data.

DATAFRAME
STRUCTURE
COLUMNS PLAYERNAM IPLTEAM BASEPRICEI
E NCR

0 ROHIT MI 13

1 VIRAT RCB 17

2 HARDIK MI 14

INDEX DATA

PROPERTIES OF DATAFRAME

1. A Dataframe has axes (indices)-


Row index (axis=0)
Column index (axes=1)
It is similar to a spreadsheet , whose row index is called index and
column index is called column name.
A Dataframe contains Heterogeneous data.
A Dataframe Size is Mutable.
A Dataframe Data is Mutable.
A data frame can be created using any of the following-

1. Series
2. Lists
3. Dictionary
4. A numpy 2D array

How to create Dataframe From Series

Program-
Output-
import pandas as pd
0
s = [Link](['a','b','c','d']) 0 a
df=[Link](s) 1 b Default Column Name As 0
2 c
print(df)
3 d
DataFrame from Dictionary of Series

Example-

DataFrame from List of Dictionaries

Example-
Iteration on Rows and Columns

If we want to access record or data from a data frame row wise or column
wise then iteration is used. Pandas provide 2 functions to perform iterations-

1. iterrows ()
2. iteritems ()

iterrows()

It is used to access the data row wise. Example-


iteritems()

It is used to access the data column wise.


Example-
To Add & Rename a column in data
frame
import pandas as pd

s = [Link]([10,15,18,22])
df=[Link](s)
[Link]=[‘List1’] To Rename the default column of Data
Frame as List1
df[‘List2’]=20 To create a new column List2 with all valuesas
20

df[‘List3’]=df[‘List1’]+df[‘List2’] Output-

Add Column1 and Column2 and store in List1 List2


List3 0 10 20
30
New column List3 1 15 20 35
2 18 20 38
print(df) 3 22 20 42
To Delete a Column in data frame
We can delete the column from a data frame by using any ofthe
the following –
1. del
2. pop()
3. drop()

>>del df[‘List3’] We can simply delete a column by passing


column name in subscript with df
>>df
Output-

List1 List
2
0 10 20
1 15 20
2 18 20
3 22 20

>>[Link](‘List2’) we can simply delete a column by passing


column name in pop method.
>>df

List1
0 10
1 15
2 18
3 22
To Delete a Column Using drop()
import pandas as pd
s= [Link]([10,20,30,40])
df=[Link](s)
[Link]=[‘List1’]
df[‘List2’]=40
df1=[Link](‘List2’,axis=1) (axis=1) means to delete Data
column wise
df2=[Link](index=[2,3],axis=0) (axis=0) means to delete
data row wise with given index
print(df)
print(“ After deletion::”)
print(df1)
print (“ After row deletion::”)
print(df2)

Output-
List1 List
2
0 10 40
1 20 40
2 30 40
3 40 40
After
deletion::
List1
0 10
1 20
2 30
3 40
After row deletion::
List1
0 10
1 20
Accessing the data frame through loc() and
iloc() method or indexing using Labels
Pandas provide loc() and iloc() methods to access the subset from adata
frame using row/column.

Accessing the data frame through loc()

It is used to access a group of rows and columns.


Syntax-
[Link][StartRow : EndRow, StartColumn : EndColumn]
Note -If we pass : in row or column part then pandas provide the entirerows
or columns respectively.

To access a single row

To access multiple Rows Qtr1 to Qtr3


Accessing the data frame through iloc()

It is used to access a group of rows and columns based on numeric


index value.
Syntax-

[Link][StartRowindexs : EndRowindex, StartColumnindex : EndColumnindex]

To access First two Rows


and Second column

To access all Rows and First


Two columns Record

Note -If we pass : in row or column part then pandas providethe


entire rows or columns respectively.

You might also like