SlideShare a Scribd company logo
1 of 28
PANDAS SERIES
Data Handling using Pandas – 1 Syllabus
Series: Creation of Series from – ndarray, dictionary, scalar value;
mathematical operations; Head and Tail functions; Selection,
Indexing and Slicing.
1
Sangita Panchal
Series
 A pandas Series is a one-dimensional array
with homogeneous data.
 The axis labels are collectively called Index.
The general format:
pandas. Series (data, index, dtype)
data: It is like ndarray, list, constants
index: It is unique, by default 0..n-1 (np.arange(n))
dtype: data type
2
Sangita Panchal
Key Features:
• Homogeneous data
• Size immutable
• Values of data mutable
Creation of Series
A series can be created using: array, dict, scalar value or constant.
3
Sangita Panchal
import pandas as pd
s = pd.Series()
print(s)
Series([], dtype: float64)
import pandas as pd
import numpy as np
data = np.array(['a','b','c','d'])
s = pd.Series(data)
print(s) 0 a
1 b
2 c
3 d
dtype: object
Default
Index
import pandas as pd
data = ['a','b','c','d']
s = pd.Series(data)
print(s)
Creation of Series 4
Sangita Panchal
import pandas as pd
data = 100
s = pd.Series(data,index = [1,2,3])
print(s)
import pandas as pd
data = {'a':1,'b':2,'c':3}
s = pd.Series(data)
print(s)
a 1
b 2
c 3
1 100
2 100
3 100
Key is declared
as Index
Defined your
own Index
Creation of Series – give a name to the series 5
Sangita Panchal
a.name = "Marks"
print(a)
import pandas as pd
data = [20,25,19,12,17]
a = pd.Series(data,name = "Rollno")
print(a)
0 20
1 25
2 19
3 12
4 17
Name: Rollno, dtype: int64
0 20
1 25
2 19
3 12
4 17
Name: Marks, dtype: int64
Series name is Rollno
Series name is Marks
Pandas Series – Created by naming index, data
import pandas as pd
s = pd.Series(index = ['Ram','Joe','Nia'],data = [1,2,3])
s1 = pd.Series([1,2,3],['Ram','Joe','Nia'])
print(s)
print(s1)
6
Sangita Panchal
Ram 1
Joe 2
Nia 3
dtype: int64
Output
Pandas Series – isnull()
import pandas as pd
data = {'apples':500,'kiwi':20,'oranges':100}
a = ['apples’, 'banana’, 'kiwi’, 'oranges']
b = pd.Series(data, index = a)
print(b)
print(b. isnull())
7
Sangita Panchal
apples 500.0
banana NaN
kiwi 20.0
oranges 100.0
dtype: float64
apples False
banana True
kiwi False
oranges False
dtype: bool
Output
Arithmetic operations on Series (Scalar /Vector) 8
Sangita Panchal
Operation Operator Function/Method
Addition + add()
Subtraction - sub() /subtract()
Multiplication * mul() / multiply()
Division / div() / divide()
Floor Division //
Remainder %
Exponent ** pow()
Arithmetic operations on Series (Scalar /Vector)
performed on all the values in a Series
9
Sangita Panchal
Operation Function/Method
Add all values sum()
Multiply all values prod()
Mean of all values mean()
Maximum max()
Minimum min()
Count Non- NA/Null values count()
return N elements from top(default is 5) head()
return N elements from bottom(default is 5) tail()
return sorted series (ascending = True / False) sort_values()
Pandas Series (max, min, count, sum, mean)
import pandas as pd
data = [11, 9, 24, 17,15]
a = pd.Series(data)
print(data)
print("Total",a.sum())
print("Mean",a.mean())
print("Maximum",a.max())
print("Minimum",a.min())
print("Count",a.count())
10
Sangita Panchal
[11, 9, 24, 17, 15]
Total 76
Mean 15.2
Maximum 24
Minimum 9
Count 5
import pandas as pd
data = [11, 9, None, 17,15]
a = pd.Series(data)
print(data)
print("Total",a.sum())
print("Mean",a.mean())
print("Maximum",a.max())
print("Minimum",a.min())
print("Count",a.count())
[11, 9, None, 17, 15]
Total 52.0
Mean 13.0
Maximum 17.0
Minimum 9.0
Count 4
Output
Arithmetic operations on Series(Scalar/Vector)
import pandas as pd
s = pd.Series([18,None,23],['Ram','Joe','Nia’])
value = 10
print(s)
print(s+value)
11
Sangita Panchal
Ram 18.0
Joe NaN
Nia 23.0
dtype: float64
Ram 28.0
Joe NaN
Nia 33.0
dtype: float64
Output
Arithmetic operations on Series(Scalar/Vector)
import pandas as pd
s = pd.Series([18,None,23],['Ram','Joe','Nia'])
s1 = pd.Series([23,25,19],['Ram','Joe','Nia'])
s2 = s + s1
print(s2)
s = s.add(s1,fill_value = 0)
print(s)
12
Sangita Panchal
Ram 41.0
Joe NaN
Nia 42.0
dtype: float64
Ram 41.0
Joe 25.0
Nia 42.0
dtype: float64
Output
append() and drop()
import pandas as pd
data = [23,19,21]
index = ['Ram','Joe','Nia']
comp1 = pd.Series(data,index)
comp2 = pd.Series([20],['Mona'])
comp = comp1.append(comp2)
print(comp)
comp['Joe'] = 24
comp.loc['Nia'] = 25
print(comp)
comp = comp.drop(['Ram','Mona'])
print(comp)
13
Sangita Panchal
Ram 23
Joe 19
Nia 21
Mona 20
dtype: int64
Ram 23
Joe 24
Nia 25
Mona 20
dtype: int64
Joe 24
Nia 25
dtype: int64
Output
Add two Series
import pandas as pd
sub1 = [23,19,21]
sub2 = [12,23,19]
a = pd.Series(sub1)
print(a)
b = pd.Series(sub2)
print(b)
total = a + b
c = pd.Series(total)
print(c)
per = total/50 * 100
print(per)
print(per.sort_values(ascending = False))
14
Sangita Panchal
0 23
1 19
2 21
dtype: int64
0 12
1 23
2 19
dtype: int64
0 35
1 42
2 40
dtype: int64
Output
0 70.0
1 84.0
2 80.0
dtype: float64
1 84.0
2 80.0
0 70.0
dtype: float64
sort_values()
import pandas as pd
data = [11, 9, 19,17,15]
a = pd.Series(data)
print(a)
b = a.sort_values()
print("sorted",b, sep = "n")
15
Sangita Panchal
original
0 11
1 9
2 19
3 17
4 15
dtype: int64
sorted
1 9
0 11
4 15
3 17
2 19
dtype: int64
Output
head(), tail() 16
Sangita Panchal
By default, head() returns top 5 records and tail() returns bottom 5 records.
0 9
1 11
2 15
3 17
4 19
5 20
6 25
7 19
8 12
9 17
dtype: int64
dtype: int64
0 9
1 11
2 15
3 17
4 19
dtype: int64
5 20
6 25
7 19
8 12
9 17
import pandas as pd
data = [9,11,15,17,19,20,25,19,12,17]
a = pd.Series(data)
print(a)
print(a.head())
print(a.tail())
print(a.head(2))
print(a.tail(3))
dtype: int64
0 9
1 11
dtype: int64
7 19
8 12
9 17
Output
Accessing data from Series (Indexing and Slicing) 17
Sangita Panchal
import pandas as pd
data = [1,2,3]
s = pd.Series(data,index = ['Ram','Joe','Nia'])
print(s)
print(s[1])
print(s[1:2])
print(s['Ram'])
print(s[['Ram','Joe']])
Ram 1
Joe 2
Nia 3
dtype: int64
2
Joe 2
dtype: int64
1
Ram 1
Joe 2
dtype: int64
Output
Boolean Indexing
import pandas as pd
data = [9,11,15,17,19,20,25,19,12,17]
a = pd.Series(data)
print([a>20])
print(a[a>=20])
print(a[(a >= 15) & (a <= 20)])
print(a[~(a <= 20)])
18
Sangita Panchal
5 20
6 25
dtype: int64
[0 False
1 False
2 False
3 False
4 False
5 False
6 True
7 False
8 False
9 False
dtype: bool]
2 15
3 17
4 19
5 20
7 19
9 17
dtype: int64
6 25
dtype: int64
Output
~ NOT
& AND
| OR
CBSE QUESTIONS
1. Given a Pandas series called Sequences, the command which will display the first 4 rows
is __________________.
a. print(Sequences.head(4))
b. print(Sequences.Head(4))
c. print(Sequences.heads(4)
d. print(Sequences.Heads(4))
2. Given the following Series S1 and S2:
Write a command to find the sum of the series.
19
Sangita Panchal
CBSE QUESTIONS
1. Given a Pandas series called Sequences, the command which will display the first 4 rows
is __________________.
a. print(Sequences.head(4))
b. print(Sequences.Head(4))
c. print(Sequences.heads(4)
d. print(Sequences.Heads(4))
2. Given the following Series S1 and S2:
Write a command to find the sum of the series.
20
Sangita Panchal
print(Sequences.head(4))
print(S1 + S2)
print(S1.add(S2))
CBSE QUESTIONS
3. Consider a given Series , M1:
Write a program in Python Pandas to create the series.
.
4. Consider the following Series Object S_amt:
ii. Write the command which will display the name of the furniture having rent>250.
ii. Write the command to name the series as Furniture.
21
Sangita Panchal
CBSE QUESTIONS
3. Consider a given Series , M1:
Write a program in Python Pandas to create the series.
.
4. Consider the following Series Object S_amt:
ii. Write the command which will display the name of the furniture having rent>250.
ii. Write the command to name the series as Furniture.
22
Sangita Panchal
import pandas as pd
Marks = [45,65,24,89]
index = [‘Term1’,’Term2’,’Term3’,’Term4’]
M1 = pd.Series(Marks, index)
print(S_amt[S_amt > 250])
S_amt.name = ‘Furniture’
CBSE QUESTIONS
5. Consider two objects x and y. x is a list whereas y is a Series. Both have values
20, 40,90, 110.
What will be the output of the following two statements considering that the
above objects have been created already
a. print (x*2)
b. print(y*2)
Justify your answer.
23
Sangita Panchal
CBSE QUESTIONS
5. Consider two objects x and y. x is a list whereas y is a Series. Both have values
20, 40,90, 110.
What will be the output of the following two statements considering that the
above objects have been created already
a. print (x*2)
b. print(y*2)
Justify your answer.
24
Sangita Panchal
x is a list
[20,40,90,110, 20,40,90,110]
y is a series
0 40
1 80
2 180
3 220
CBSE QUESTIONS
Given the following Series eng and maths, answer the questions that follow:
eng maths
(i) Create a new series total by adding the corresponding values of both series.
The NaN will be added as zero (0).
(ii) Create a new series avg to store the average of eng and maths marks.
25
Sangita Panchal
CBSE QUESTIONS
Given the following Series eng and maths, answer the questions that follow:
eng maths
(i) Create a new series total by adding the corresponding values of both series.
The NaN will be added as zero (0).
(ii) Create a new series avg to store the average of eng and maths marks.
avg = total / 2
26
Sangita Panchal
total = eng.add(maths,fill_value=0)
avg = total / 2
Pandas Series

More Related Content

What's hot

What's hot (20)

Pandas
PandasPandas
Pandas
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
 
Packages In Python Tutorial
Packages In Python TutorialPackages In Python Tutorial
Packages In Python Tutorial
 
Sets in python
Sets in pythonSets in python
Sets in python
 
Python Scipy Numpy
Python Scipy NumpyPython Scipy Numpy
Python Scipy Numpy
 
pandas - Python Data Analysis
pandas - Python Data Analysispandas - Python Data Analysis
pandas - Python Data Analysis
 
Chapter 03 python libraries
Chapter 03 python librariesChapter 03 python libraries
Chapter 03 python libraries
 
Introduction to NumPy
Introduction to NumPyIntroduction to NumPy
Introduction to NumPy
 
Numpy tutorial
Numpy tutorialNumpy tutorial
Numpy tutorial
 
Chapter 14 strings
Chapter 14 stringsChapter 14 strings
Chapter 14 strings
 
Python Pandas.pdf
Python Pandas.pdfPython Pandas.pdf
Python Pandas.pdf
 
MySQL Functions
MySQL FunctionsMySQL Functions
MySQL Functions
 
Introduction to Python Pandas for Data Analytics
Introduction to Python Pandas for Data AnalyticsIntroduction to Python Pandas for Data Analytics
Introduction to Python Pandas for Data Analytics
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in python
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
 
Map, Filter and Reduce In Python
Map, Filter and Reduce In PythonMap, Filter and Reduce In Python
Map, Filter and Reduce In Python
 
Python: Basic Inheritance
Python: Basic InheritancePython: Basic Inheritance
Python: Basic Inheritance
 
Python my sql database connection
Python my sql   database connectionPython my sql   database connection
Python my sql database connection
 
A Prelude of Purity: Scaling Back ZIO
A Prelude of Purity: Scaling Back ZIOA Prelude of Purity: Scaling Back ZIO
A Prelude of Purity: Scaling Back ZIO
 
Purely Functional Data Structures in Scala
Purely Functional Data Structures in ScalaPurely Functional Data Structures in Scala
Purely Functional Data Structures in Scala
 

Similar to Pandas Series

1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
rushabhshah600
 

Similar to Pandas Series (20)

phgv.pptx.pptx
phgv.pptx.pptxphgv.pptx.pptx
phgv.pptx.pptx
 
Basic Analysis using Python
Basic Analysis using PythonBasic Analysis using Python
Basic Analysis using Python
 
Python Library-Series.pptx
Python Library-Series.pptxPython Library-Series.pptx
Python Library-Series.pptx
 
Python programming workshop session 3
Python programming workshop session 3Python programming workshop session 3
Python programming workshop session 3
 
Presentation on Pandas in _ detail .pptx
Presentation on Pandas in _ detail .pptxPresentation on Pandas in _ detail .pptx
Presentation on Pandas in _ detail .pptx
 
Programs in array using SWIFT
Programs in array using SWIFTPrograms in array using SWIFT
Programs in array using SWIFT
 
Lecture 9.pptx
Lecture 9.pptxLecture 9.pptx
Lecture 9.pptx
 
Python High Level Functions_Ch 11.ppt
Python High Level Functions_Ch 11.pptPython High Level Functions_Ch 11.ppt
Python High Level Functions_Ch 11.ppt
 
interenship.pptx
interenship.pptxinterenship.pptx
interenship.pptx
 
Python-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptxPython-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptx
 
LIST IN PYTHON-PART 3[BUILT IN PYTHON]
LIST IN PYTHON-PART 3[BUILT IN PYTHON]LIST IN PYTHON-PART 3[BUILT IN PYTHON]
LIST IN PYTHON-PART 3[BUILT IN PYTHON]
 
Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLAB
 
BUilt in Functions and Simple programs in R.pdf
BUilt in Functions and Simple programs in R.pdfBUilt in Functions and Simple programs in R.pdf
BUilt in Functions and Simple programs in R.pdf
 
Unit 3_Numpy_Vsp.pptx
Unit 3_Numpy_Vsp.pptxUnit 3_Numpy_Vsp.pptx
Unit 3_Numpy_Vsp.pptx
 
Python Modules and Libraries
Python Modules and LibrariesPython Modules and Libraries
Python Modules and Libraries
 
Python Modules, Packages and Libraries
Python Modules, Packages and LibrariesPython Modules, Packages and Libraries
Python Modules, Packages and Libraries
 
PyData Paris 2015 - Track 1.2 Gilles Louppe
PyData Paris 2015 - Track 1.2 Gilles LouppePyData Paris 2015 - Track 1.2 Gilles Louppe
PyData Paris 2015 - Track 1.2 Gilles Louppe
 
The Ring programming language version 1.6 book - Part 183 of 189
The Ring programming language version 1.6 book - Part 183 of 189The Ring programming language version 1.6 book - Part 183 of 189
The Ring programming language version 1.6 book - Part 183 of 189
 
PPT on Data Science Using Python
PPT on Data Science Using PythonPPT on Data Science Using Python
PPT on Data Science Using Python
 
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
 

Recently uploaded

Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 

Recently uploaded (20)

21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactistics
 
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 

Pandas Series

  • 2. Data Handling using Pandas – 1 Syllabus Series: Creation of Series from – ndarray, dictionary, scalar value; mathematical operations; Head and Tail functions; Selection, Indexing and Slicing. 1 Sangita Panchal
  • 3. Series  A pandas Series is a one-dimensional array with homogeneous data.  The axis labels are collectively called Index. The general format: pandas. Series (data, index, dtype) data: It is like ndarray, list, constants index: It is unique, by default 0..n-1 (np.arange(n)) dtype: data type 2 Sangita Panchal Key Features: • Homogeneous data • Size immutable • Values of data mutable
  • 4. Creation of Series A series can be created using: array, dict, scalar value or constant. 3 Sangita Panchal import pandas as pd s = pd.Series() print(s) Series([], dtype: float64) import pandas as pd import numpy as np data = np.array(['a','b','c','d']) s = pd.Series(data) print(s) 0 a 1 b 2 c 3 d dtype: object Default Index import pandas as pd data = ['a','b','c','d'] s = pd.Series(data) print(s)
  • 5. Creation of Series 4 Sangita Panchal import pandas as pd data = 100 s = pd.Series(data,index = [1,2,3]) print(s) import pandas as pd data = {'a':1,'b':2,'c':3} s = pd.Series(data) print(s) a 1 b 2 c 3 1 100 2 100 3 100 Key is declared as Index Defined your own Index
  • 6. Creation of Series – give a name to the series 5 Sangita Panchal a.name = "Marks" print(a) import pandas as pd data = [20,25,19,12,17] a = pd.Series(data,name = "Rollno") print(a) 0 20 1 25 2 19 3 12 4 17 Name: Rollno, dtype: int64 0 20 1 25 2 19 3 12 4 17 Name: Marks, dtype: int64 Series name is Rollno Series name is Marks
  • 7. Pandas Series – Created by naming index, data import pandas as pd s = pd.Series(index = ['Ram','Joe','Nia'],data = [1,2,3]) s1 = pd.Series([1,2,3],['Ram','Joe','Nia']) print(s) print(s1) 6 Sangita Panchal Ram 1 Joe 2 Nia 3 dtype: int64 Output
  • 8. Pandas Series – isnull() import pandas as pd data = {'apples':500,'kiwi':20,'oranges':100} a = ['apples’, 'banana’, 'kiwi’, 'oranges'] b = pd.Series(data, index = a) print(b) print(b. isnull()) 7 Sangita Panchal apples 500.0 banana NaN kiwi 20.0 oranges 100.0 dtype: float64 apples False banana True kiwi False oranges False dtype: bool Output
  • 9. Arithmetic operations on Series (Scalar /Vector) 8 Sangita Panchal Operation Operator Function/Method Addition + add() Subtraction - sub() /subtract() Multiplication * mul() / multiply() Division / div() / divide() Floor Division // Remainder % Exponent ** pow()
  • 10. Arithmetic operations on Series (Scalar /Vector) performed on all the values in a Series 9 Sangita Panchal Operation Function/Method Add all values sum() Multiply all values prod() Mean of all values mean() Maximum max() Minimum min() Count Non- NA/Null values count() return N elements from top(default is 5) head() return N elements from bottom(default is 5) tail() return sorted series (ascending = True / False) sort_values()
  • 11. Pandas Series (max, min, count, sum, mean) import pandas as pd data = [11, 9, 24, 17,15] a = pd.Series(data) print(data) print("Total",a.sum()) print("Mean",a.mean()) print("Maximum",a.max()) print("Minimum",a.min()) print("Count",a.count()) 10 Sangita Panchal [11, 9, 24, 17, 15] Total 76 Mean 15.2 Maximum 24 Minimum 9 Count 5 import pandas as pd data = [11, 9, None, 17,15] a = pd.Series(data) print(data) print("Total",a.sum()) print("Mean",a.mean()) print("Maximum",a.max()) print("Minimum",a.min()) print("Count",a.count()) [11, 9, None, 17, 15] Total 52.0 Mean 13.0 Maximum 17.0 Minimum 9.0 Count 4 Output
  • 12. Arithmetic operations on Series(Scalar/Vector) import pandas as pd s = pd.Series([18,None,23],['Ram','Joe','Nia’]) value = 10 print(s) print(s+value) 11 Sangita Panchal Ram 18.0 Joe NaN Nia 23.0 dtype: float64 Ram 28.0 Joe NaN Nia 33.0 dtype: float64 Output
  • 13. Arithmetic operations on Series(Scalar/Vector) import pandas as pd s = pd.Series([18,None,23],['Ram','Joe','Nia']) s1 = pd.Series([23,25,19],['Ram','Joe','Nia']) s2 = s + s1 print(s2) s = s.add(s1,fill_value = 0) print(s) 12 Sangita Panchal Ram 41.0 Joe NaN Nia 42.0 dtype: float64 Ram 41.0 Joe 25.0 Nia 42.0 dtype: float64 Output
  • 14. append() and drop() import pandas as pd data = [23,19,21] index = ['Ram','Joe','Nia'] comp1 = pd.Series(data,index) comp2 = pd.Series([20],['Mona']) comp = comp1.append(comp2) print(comp) comp['Joe'] = 24 comp.loc['Nia'] = 25 print(comp) comp = comp.drop(['Ram','Mona']) print(comp) 13 Sangita Panchal Ram 23 Joe 19 Nia 21 Mona 20 dtype: int64 Ram 23 Joe 24 Nia 25 Mona 20 dtype: int64 Joe 24 Nia 25 dtype: int64 Output
  • 15. Add two Series import pandas as pd sub1 = [23,19,21] sub2 = [12,23,19] a = pd.Series(sub1) print(a) b = pd.Series(sub2) print(b) total = a + b c = pd.Series(total) print(c) per = total/50 * 100 print(per) print(per.sort_values(ascending = False)) 14 Sangita Panchal 0 23 1 19 2 21 dtype: int64 0 12 1 23 2 19 dtype: int64 0 35 1 42 2 40 dtype: int64 Output 0 70.0 1 84.0 2 80.0 dtype: float64 1 84.0 2 80.0 0 70.0 dtype: float64
  • 16. sort_values() import pandas as pd data = [11, 9, 19,17,15] a = pd.Series(data) print(a) b = a.sort_values() print("sorted",b, sep = "n") 15 Sangita Panchal original 0 11 1 9 2 19 3 17 4 15 dtype: int64 sorted 1 9 0 11 4 15 3 17 2 19 dtype: int64 Output
  • 17. head(), tail() 16 Sangita Panchal By default, head() returns top 5 records and tail() returns bottom 5 records. 0 9 1 11 2 15 3 17 4 19 5 20 6 25 7 19 8 12 9 17 dtype: int64 dtype: int64 0 9 1 11 2 15 3 17 4 19 dtype: int64 5 20 6 25 7 19 8 12 9 17 import pandas as pd data = [9,11,15,17,19,20,25,19,12,17] a = pd.Series(data) print(a) print(a.head()) print(a.tail()) print(a.head(2)) print(a.tail(3)) dtype: int64 0 9 1 11 dtype: int64 7 19 8 12 9 17 Output
  • 18. Accessing data from Series (Indexing and Slicing) 17 Sangita Panchal import pandas as pd data = [1,2,3] s = pd.Series(data,index = ['Ram','Joe','Nia']) print(s) print(s[1]) print(s[1:2]) print(s['Ram']) print(s[['Ram','Joe']]) Ram 1 Joe 2 Nia 3 dtype: int64 2 Joe 2 dtype: int64 1 Ram 1 Joe 2 dtype: int64 Output
  • 19. Boolean Indexing import pandas as pd data = [9,11,15,17,19,20,25,19,12,17] a = pd.Series(data) print([a>20]) print(a[a>=20]) print(a[(a >= 15) & (a <= 20)]) print(a[~(a <= 20)]) 18 Sangita Panchal 5 20 6 25 dtype: int64 [0 False 1 False 2 False 3 False 4 False 5 False 6 True 7 False 8 False 9 False dtype: bool] 2 15 3 17 4 19 5 20 7 19 9 17 dtype: int64 6 25 dtype: int64 Output ~ NOT & AND | OR
  • 20. CBSE QUESTIONS 1. Given a Pandas series called Sequences, the command which will display the first 4 rows is __________________. a. print(Sequences.head(4)) b. print(Sequences.Head(4)) c. print(Sequences.heads(4) d. print(Sequences.Heads(4)) 2. Given the following Series S1 and S2: Write a command to find the sum of the series. 19 Sangita Panchal
  • 21. CBSE QUESTIONS 1. Given a Pandas series called Sequences, the command which will display the first 4 rows is __________________. a. print(Sequences.head(4)) b. print(Sequences.Head(4)) c. print(Sequences.heads(4) d. print(Sequences.Heads(4)) 2. Given the following Series S1 and S2: Write a command to find the sum of the series. 20 Sangita Panchal print(Sequences.head(4)) print(S1 + S2) print(S1.add(S2))
  • 22. CBSE QUESTIONS 3. Consider a given Series , M1: Write a program in Python Pandas to create the series. . 4. Consider the following Series Object S_amt: ii. Write the command which will display the name of the furniture having rent>250. ii. Write the command to name the series as Furniture. 21 Sangita Panchal
  • 23. CBSE QUESTIONS 3. Consider a given Series , M1: Write a program in Python Pandas to create the series. . 4. Consider the following Series Object S_amt: ii. Write the command which will display the name of the furniture having rent>250. ii. Write the command to name the series as Furniture. 22 Sangita Panchal import pandas as pd Marks = [45,65,24,89] index = [‘Term1’,’Term2’,’Term3’,’Term4’] M1 = pd.Series(Marks, index) print(S_amt[S_amt > 250]) S_amt.name = ‘Furniture’
  • 24. CBSE QUESTIONS 5. Consider two objects x and y. x is a list whereas y is a Series. Both have values 20, 40,90, 110. What will be the output of the following two statements considering that the above objects have been created already a. print (x*2) b. print(y*2) Justify your answer. 23 Sangita Panchal
  • 25. CBSE QUESTIONS 5. Consider two objects x and y. x is a list whereas y is a Series. Both have values 20, 40,90, 110. What will be the output of the following two statements considering that the above objects have been created already a. print (x*2) b. print(y*2) Justify your answer. 24 Sangita Panchal x is a list [20,40,90,110, 20,40,90,110] y is a series 0 40 1 80 2 180 3 220
  • 26. CBSE QUESTIONS Given the following Series eng and maths, answer the questions that follow: eng maths (i) Create a new series total by adding the corresponding values of both series. The NaN will be added as zero (0). (ii) Create a new series avg to store the average of eng and maths marks. 25 Sangita Panchal
  • 27. CBSE QUESTIONS Given the following Series eng and maths, answer the questions that follow: eng maths (i) Create a new series total by adding the corresponding values of both series. The NaN will be added as zero (0). (ii) Create a new series avg to store the average of eng and maths marks. avg = total / 2 26 Sangita Panchal total = eng.add(maths,fill_value=0) avg = total / 2