Python Pandas Series Notes | Pandas Series IP 12

Python Pandas Series Notes

In this article, We will get very handy Python Pandas Series Notes. We all know Python pandas is one of the most important library in python programming used to manage large and complex data using various data structures such as Panel, Series, Dataframes. This article titled ‘ python pandas series notes ‘ talks about how to declare and manage python pandas series with examples.
Please refer following table of content listed all the topics for python pandas series notes.

Contents

Series Data Structure

  • It is 1D data structure
  • It is size immutable
  • It is value mutable
  • It stores homogeneous data
  • It supports explicit indexing

Create or Declare Series object

To create series object we need to do followings

Import pandas library in your program as per syntax given below:

import pandas as <object name>
For exp –
import pandas as pd

Use Series() method of pandas library to create series object as per syntax given below:

<Series object> = <panda_object>.Series( data = <value>, index = <value>)
For exp –
S = pd.Series(data = [12,23,34],index = [‘a’,’b’,’c’])

While creating Series object using Series() method, following are the points we should keep in our mind:

  • Series() can have two arguments data and index
  • Series() arguments can be taken in any order.
  • Index argument is optional
  • It can be made on any data such as python sequence (List, Tuple, String), ndarray, dictionary, scalar value
  • We can assign value of any data type as Index.
  • We can skip keyword data for assigning values to series object.

Creating Series object using List-

Syntax:

<Series object> = <panda_object>.Series(<any list of values>)

Without Indexing-

Exp-1:

 import pandas as pd
 S = pd.Series([23,34,55])
 print(“My Series is”)
 print(S) 

Output –

 My Series is
 0  23
 1  34
 2  55
 dtype : int64 
With index –

Exp-2:

 import pandas as pd
 S = pd.Series([23,34,55], index = [‘jan’,’feb’,’mar’])
 print(“My Series is”)
 print(S) 

Output –

 My Series is
 jan       23
 feb       34
 mar       55
 dtype : int64 
Exp-1: Create a Series object using a list that stores the total no of students of three sections ‘A’,’B’,’C.
 import pandas as pd
 section = [‘A’,’B’,‘C’]
 students = [40,50,45]
 S = pd.Series(students, index = section)
 print(“My Series is”)
 print(S) 

Output –

 My Series is
 A  40
 B  50
 C  45
 dtype : int64 

Creating Series object Numpy Array

To use Numpy array as data for Series object make sure Numpy library is imported as per syntax given below:

import numpy as np

Syntax:

<Series object> = <panda_object>.Series(data = <nparray>, index = <sequence>)

Without Indexing-

Prog-1:

 import pandas as pd
 import Numpy as np
 n = np.array([10.8,12.6,8.2])
 S = pd.Series(n)
 print(“My Series is”)
 print(S) 

Output –

 My Series is
 0  10.8
 1  12.6
 2  8.2
 dtype : float64 
With index –

Prog-2:

 import pandas as pd
 import numpy as np
 n = np.arrange(10,50,10)
 S = pd.Series(n, index = range(1,5))
 print(“My Series is”)
 print(S) 

Output –

 My Series is
 1         10
 2         20
 3         30
 4         40    
 dtype : int32 
Exp2: Create a Series object that stores amount paid as values and name of customer as index. Amount paid is taken as nparray.
 import pandas as pd
 import numpy as np
 amount = np.array([ 200.6, 350.4, 760.2])
 names = [‘raj’, ‘sunil’, ‘kamal’]
 S = pd.Series(amount, index = names)
 print(“My Series is”)
 print(S) 

Output –

 My Series is
 Raj       200.6
 Sunil     350.4
 kamal     760.2
 dtype : float64 

Creating Series object using Scalar value

  • Scalar value refers to single value passed for creating series object.
  • Index argument must be passed while creating series object using scalar value.

Syntax:

<Series object> = <panda_object>.Series(scalar value, index = <sequence>)

Prog-1:

 import pandas as pd
 S = pd.Series(2021, index= [‘jan’,’feb’,’mar’])
 print(“My Series is”)
 print(S) 

Output –

 My Series is
 Jan        2021
 Feb        2021
 Mar        2021
 dtype : int64 

Prog-2:

 import pandas as pd
 S = pd.Series(2000, index= range(5))
 print(“My Series is”)
 print(S) 

Output –

 My Series is
 0         2000
 1         2000
 2         2000
 3         2000
 dtype : int64 

Creating Series object using String

Syntax:

<Series object> = <panda_object>.Series(<string value>, index = <sequence>)

Exp-3: Create a series object using individual characters
 import pandas as pd
 S = pd.Series([‘m’,’o’,’l’])
 print(“My Series is”)
 print(S) 

Output –

 My Series is
 0         m
 1         o
 2         l            
 dtype : object 
Exp-4: Create a series object using String
 import pandas as pd
 S = pd.Series(‘techtipnow’)
 print(“My Series is”)
 print(S) 

Output –

 My Series is
 0         techtipnow
 dtype : object 
Exp-5: Create a series object using multiple Strings
 import pandas as pd
 S = pd.Series([‘I’,’am’,’Indian’l, index = [1,2,3])
 print(“My Series is”)
 print(S) 

Output –

 My Series is
 1         I
 2         am
 3         Indian       
 dtype : object 

Creating Series object using Dictionaries

  • Key of Dictionary is always treated as index of Series object
  • Values of Dictionary are always treated as data of Series object.

Syntax:

<Series object> = <panda_object>.Series(<dictionary object>)

Exp-6: Create a series object using using dictionary that stores no of votes secured by each party in election 21-22
 import pandas as pd
 d={‘bjp’:234,’inc’:210,’jdu’:80}
 S = pd.Series(d)
 print(“My Series is”)
 print(S) 

Output –

 My Series is
 Bjp        234
 INC       210
 jdu        80           
 dtype : int64 

Specifying Missing values in Series object

To represent missing values for Series objects, We can use –

  • None
  • np.NaN
Exp-7: Create a series object using using dictionary that stores no of votes secured by each party in election 21-22. Make sure Nota is included with missing  value.
 import pandas as pd
 d={‘bjp’:234,’inc’:210,’jdu’:80, ‘nota’: None}
 S = pd.Series(d)
 print(“My Series is”)
 print(S) 

Output –

 My Series is
 Bjp        234
 INC       210
 jdu       80
 nota      NaN          
 dtype : float64 
Exp-8: Create a series object using tuple that stores month wise percentage of absent students of class 12th . Missing value should be represented properly
 import pandas as pd
 import numpy as np
 month = [‘jan’,’feb’,’mar’]
 attend = (50,np.nan,70)
 S = pd.Series(d)
 print(“My Series is”)
 print(S) 

Output –

 My Series is
 Jan       50
 Feb       NaN
 Mar       70           
 dtype : float64 

Mathematical Expressions to Create Data in Series

We can provide data for Series() method by implementing mathematical expression that can calculate values for Data sequence as per syntax given below:

 <Series object> = <panda_object>.Series(<mathematical expression>, index = <sequence>)

Prog-1:

 import pandas as pd
 import numpy as np
 n = np.array([12.2,23.3,40.0])
 S = pd.Series(n*2, index = range(1,4))
 print(“My Series is”)
 print(S) 

Output –

 My Series is
 1         24.4
 2         46.6         
 3         80.0         
 dtype : float64 

Note: In numpy array n*2 will be applied to each value of n.

Prog-2:

 import pandas as pd
 L = [12,23,None]
 S = pd.Series(L*2)
 print(“My Series is”)
 print(S) 

Output –

 My Series is
0     12.0
1      23.0
2      NaN
3      12.0
4      23.0
5      NaN
 dtype : float64 

Note: Here L*2 will replicate values of L two times.

Accessing Elements of Series Object

We can access elements of Series object in two ways –

  • Index: Using Index we can access
    • Using Indexing we can access Individual element of Series object.
    • Using Indexing we can access multiple elements of Series object that may not be contiguous element.
    • Indexing can be used in two ways : Positional Index, Labelled Index
    • In Positional Index an Integer value is taken which represent specific element.
    • In Labelled Index any user defined label as index is taken.
  • Slice: Using Slice we can access
    • Subset of Series object contain multiple elements are always contiguous element.
Accessing Individual Element

To access individual element, we have to provide index no of the element within square bracket of Series object.

Syntax:

<Series object>[<Index>]

Prog-1

 import pandas as pd
 S = pd.Series(range(10,101,10))
 print(“We have Accessed”)
 print(S[4]) 

Output –

 We have Accessed
 50 
Accessing Multiple Elements Using Index

To access multiple elements, we have to provide index no of the each element as List within square bracket of Series object .

Syntax:

<Series object>[[<Index, Index,…>]]

Prog-1

 import pandas as pd
 S = pd.Series([12,23,34,45,55],index = [‘m1’,’m2’,’m3’,’m4’,’m5’])
 print(“We have Accessed”)
 print(S[[‘m1’,’m4’]]) 

Output –

 We have Accessed
 m1        12
 m2        45
 dtype:    int64 

Slicing

  • Extracting a specific part of Series object is called Slicing.
  • Subset occurred after slicing contains contiguous elements.
  • Slicing is done using positions not Index of the Series object.
  • In Positional slicing value of end index is excluded.
  • If labels are used in slicing, than value at end index label is also included.
  • Slicing can also be used to extract slice elements in reverse order.

We can retrieve subset of series object as per syntax given below –

<Series object>[start_index : end_index : step-value]

Prog-1

 import pandas as pd
 S = pd.Series([12,23,34,45,55,76,80,92,41,69,56])
 print(“Slicing demo”)
 print(S[2:6]) 

Output –

 Slicing Demo
 2         34
 3         45
 4         55
 5         76
 dtype:    int64 

Prog-2

 import pandas as pd
 S = pd.Series([12,23,34,45,55,76,80,92,41,69,56])
 print(“Slicing demo”)
 print(S[:]) 

Output –

 Slicing Demo
0     12
1     23
2     34
3     45
4     55
5     76
6     80
7     92
8     41
9     69
10   56
 dtype:    int64 

Prog-3

 import pandas as pd
 S = pd.Series([12,23,34,45,55,76,80,92,41,69,56])
 print(“Slicing demo”)
 print(S[:4]) 

Output –

 Slicing Demo
 0         12
 1         23
 2         34
 3         45
 dtype:    int64 

Prog-4

 import pandas as pd
 S = pd.Series([12,23,34,45,55,76,80,92,41,69,56])
 print(“Slicing demo”)
 print(S[5:]) 

Output –

 Slicing Demo
5     76
6     80
7     92
8     41
9     69
10   56
 dtype:    int64 

Prog-5

 import pandas as pd
 S = pd.Series([12,23,34,45,55,76,80,92,41,69,56])
 print(“Slicing demo”)
 print(S[1:9:2]) 

Output –

 Slicing Demo
 1         23
 3         45
 5         76
 7         92
 dtype:    int64 

Prog-6

 import pandas as pd
 S = pd.Series([12,23,34,45,55,76,80,92,41,69,56])
 print(“Slicing demo”)
 print(S[7:15]) 

Output –

 Slicing Demo
 7         92
 8         41
 9         69
 10        56
 dtype:    int64 

Note –  if end index is out of bound, even though slicing produces subset

Prog-7

 import pandas as pd
 S = pd.Series([12,23,34,45,55,76,80,92,41,69,56])
 print(“Slicing demo”)
 print(S[3:-5]) 

Output –

 Slicing Demo
 3         45
 4         55
 5         76
 dtype:    int64 

Prog-8

 import pandas as pd
 S = pd.Series([12,23,34,45,55,76,80,92,41,69,56])
 print(“Slicing demo”)
 print(S[:-6]) 

Output –

Slicing Demo
0    12
1    23
2    34
3    45
4    55
dtype:    int64 

Prog-9

 import pandas as pd
 S = pd.Series([12,23,34,45,55,76,80,92,41,69,56])
 print(“Slicing demo”)
 print(S[-4:]) 

Output –

Slicing Demo
 7         92
 8         41
 9         69
 10        56
dtype:    int64 

Prog-10

 import pandas as pd
 S = pd.Series([12,23,34,45,55,76,80,92,41,69,56])
 print(“Slicing demo”)
 print(S[-7:-2]) 

Output –

 Slicing Demo
 4         55
 5         76
 6         80
 7         92
 8         41
 dtype:    int64 

Prog-11

 import pandas as pd
 S = pd.Series([12,23,34,45,55,76,80,92,41,69,56])
 print(“Slicing demo”)
 print(S[-4:]) 

Output –

 Slicing Demo
 7         92
 8         41
 9         69
 10        56
 dtype:    int64 

Prog-12

 import pandas as pd
 S = pd.Series([12,23,34,45,55,76,80,92,41,69,56])
 print(“Slicing demo”)
 print(S[::3]) 

Output –

 Slicing Demo
 0         12
 3         45
 6         80
 9         69
 dtype:    int64 

Prog-13

 import pandas as pd
 S = pd.Series([12,23,34,45,55,76,80,92,41,69,56])
 print(“Slicing demo”)
 print(S[::-3]) 

Output –

 Slicing Demo
 10        56
 7         92
 4         55
 1         23
 dtype:    int64 

Mathematical Operation on Series object

  • We can do arithmetic operations ( +, -, *, /) on more than one series objects.
  • The arithmetic operation is performed only on matching index.
  • For non-matching index it produces NaN values.
  • If data items of matching indexes are not compatible for the operation, it produces NaN values as a result.

Prog-1

 import pandas as pd
 S1 = pd.Series([12,23,34])
 S2 = pd.Series([10,20,10])
 print(“Addition of Series with matching indexes”)
 print(S1 + S2) 

Output –

 Addition of Series with matching indexes
0     22
1     43
2     44
 dtype:    int64 

Prog-2

 import pandas as pd
 S1 = pd.Series([12,23,34,56])
 S2 = pd.Series([10,20,10])
 print(“Addition of Series of Different sizes”)
 print(S1 + S2) 

Output –

 Addition of Series of Different sizes
0    22
1    43
2    44
3    NaN
 dtype:    int64 

Prog-3

 import pandas as pd
 S1 = pd.Series([12,23,34])
 S2 = pd.Series([10,20,10],index=[‘a’,’b’,’c’])
 print(“Addition of Series With Non Matching Index”)
 print(S1 + S2) 

Output –

 Addition of Series With Non Matching Index
 0         NaN
 1         NaN
 2         NaN
 a         NaN
 b         NaN
 c         NaN
 dtype:    float64 

Attributes of Series Object

AttributeDescription
indexReturns index of Series object
valuesReturns series as nparray
shapeReturns size of Series object as tuple
sizeReturn no of element of Series object
hasnansReturns True if any NaN value exists in Series object otherwise False
emptyReturns empty if Series object has no element
dtypeReturns the data type of Series object
Exp-9 : Retrieving Index of Series object
 import pandas as pd
 S = pd.Series([12,23,34,45,55,76,80,92,41,69,56])
 print(“Retrieving Index Array”)
 print(S.index) 

Output –

 Retrieving Index Array
 RangeIndex(start=0, stop=11, step=1) 
Exp-10 : Retrieving Labelled Index of Series object
 import pandas as pd
 S = pd.Series([10,20,10],index=[‘a’,’b’,’c’])
 print(“Retrieving Label of Series OBject”)
 print(S.index) 

Output –

 Retrieving Label of Series OBject
 Index(['a', 'b', 'c'], dtype='object') 
Exp-11 : returning Series as numpy array
 import pandas as pd
 S = pd.Series([12,23,34,45,55,76,80,92,41,69,56])
 print(“Returning Series As Numpy Array”)
 print(S.values) 

Output –

 Returning Series As Numpy Array
 array([12, 23, 34, 45, 55, 76, 80, 92, 41, 69, 56], dtype=int64) 
Exp-12 : Returning total no of elements including missing values as tuple of Series object
 import pandas as pd
 S = pd.Series([12,23,34,45,55,76,80,92,41,69,56])
 print(“Returning Shape of Series”)
 print(S.shape) 

Output –

 Returning Shape of Series
 (11,) 
Exp-13 : Returning total no of elements including missing values as tuple of Series object
 import pandas as pd
 S = pd.Series([12,23,34,45,55,76,80,92,41,69,56])
 print(“Returning Shape of Series”)
 print(S.shape) 

Output –

 Returning Shape of Series
 (11,) 
Exp-14 : Returning total no of elements including missing values as integer of Series object
 import pandas as pd
 S = pd.Series([12,23,34,45,55,76,80,92,41,69,56])
 print(“Returning Size of Series”)
 print(S.size) 

Output –

 Returning Size of Series
 11 
Exp-15 : Check for any missing value in Series object
 import pandas as pd
 S = pd.Series([12,23,34,None,56])
 print(“Check Missing Value”)
 print(S.hasnans) 

Output –

 Check for Missing value
 True 
Exp-16 : Check Series object is empty or not
 import pandas as pd
 S = pd.Series([12,23,34,None,56])
 print(“Series Emptiness”)
 print(S.empty) 

Output –

 Series Emptiness
 False 
Exp-17 : Check data type of individual element of Series object
 import pandas as pd
 S = pd.Series([12,23,34,65,56])
 print(“Check data type of Series object”)
 print(S.dtype) 

Output –

 Check data type of Series object
 dtype('int64') 
Exp-18 : Check data type of individual element of Series object
 import pandas as pd
 S = pd.Series([12,23,None,65,56])
 print(“Check data type of Series object”)
 print(S.dtype) 

Output –

 Check data type of Series object
 dtype('float64') 

Renaming Index of Series object

We can rename existing index of Series object with new one as per syntax given below:

<series_object>.index = <new index array>

Exp-19 : Renaming Index of Series object
 import pandas as pd
 S = pd.Series([10,20,10],index=[‘a’,’b’,’c’])
 print(“Series object with Original Index”)
 print(S)
 S.index = [101,102,103]
 print(“Series object After renaming Index”)
 Print(S) 

Output –

 Series object with Original Index
 a         10
 b         20
 c         10 
 Series object After renaming Index
 101       10
 102       20
 103       10 

Modifying Series Elements

We can modify series elements in two ways:

Using Index

Using Index we can modify only one element at a time as given below:

 import pandas as pd
 S = pd.Series([10,20,10],index=[‘a’,’b’,’c’])
 print(“Series object with Original Elements”)
 print(S)
 S[‘c’]=30
 print(“Series object with modified Elements”)
 Print(S) 

Output –

 Series object with Original Elements
 a         10
 b         20
 c         10
 Series object with Modified Elements
 a         10
 b         20
 c         30 
Using Slicing

We can modify multiple adjacent elements with single value using slicing of Series object as given below:

 import pandas as pd
 S = pd.Series([10,20,10,45,32,12,76])
 print(“Series object with Original Elements”)
 print(S)
 S[2:5]=30
 print(“Series object with modified Elements”)
 Print(S) 

Output –

 Series object with Original Elements
 0         10
 1         20
 2         10
 3         45
 4         32
 5         12
 6         76
 Series object with Modified Elements
 0         10
 1         20
 2         30
 3         30
 4         30
 5         12
 6         76 

Python Pandas Series MCQs

Paper Set-1 (Q1-Q25)
Paper Set-2 (Q26-Q50)
Paper Set-3 (Q51-Q75)

6 thoughts on “Python Pandas Series Notes | Pandas Series IP 12”

Leave a Comment

Your email address will not be published. Required fields are marked *

error: Content is protected !!