0% found this document useful (0 votes)
7 views19 pages

CSL 410 L14

Uploaded by

rpschauhan2003
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)
7 views19 pages

CSL 410 L14

Uploaded by

rpschauhan2003
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
You are on page 1/ 19

Program:B.

Tech(CSE) IV Semester II Year

CSL-410: Data Science using Python


Unit No. 2
Pandas: Series

Lecture No. 14

Dr. Sanjay Jain


Associate Professor, CSA/SOET
Outlines
• Introduction
• Create an Empty Series
• Create a Series from ndarray
• Create a Series from dict
• Create a Series from Scalar
• Accessing Data from Series with Position
• Retrieve Data Using Label (Index)
• Example
• References
Student Effective Learning Outcomes(SELO)
01: Ability to understand subject related concepts clearly along with
contemporary issues.
02: Ability to use updated tools, techniques and skills for effective domain
specific practices.
03: Understanding available tools and products and ability to use it
effectively.
Series: Introduction
• Series is a one-dimensional labeled array capable of holding data of any
type (integer, string, float, python objects, etc.). The axis labels are
collectively called index.

• Key Points
– Homogeneous data
– Size Immutable
– Values of Data Mutable

<SELO: 1> <Reference No.: R1,R4>


pandas.Series()
• A pandas Series can be created using the following constructor:
pandas.Series( data, index, dtype, copy)

• A series can be created using various inputs like:


– Array
– Dict
– Scalar value or constant

<SELO: 1> <Reference No.: R1,R4>


Series: Create an Empty Series
• A basic series, which can be created is an Empty Series.
• Example:
#import the pandas library and aliasing as pd
import pandas as pd
s = pd.Series()
print (s)
• Outcome:
Series([], dtype: float64)

<SELO: 1> <Reference No.: R1,R4>


Series: Create a Series from ndarray
• If data is an ndarray, then index passed must be of the same length. If no index is
passed, then by default index will be range(n) where n is array length, i.e.,
[0,1,2,3…. range(len(array))-1].
• Example:
#import the pandas library and aliasing as pd
import pandas as pd
import numpy as np
data = np.array(['a','b','c','d'])
s = pd.Series(data)
print(s)
• Outcome:
0 a
1 b
2 c
3 d
dtype: object

<SELO: 1> <Reference No.: R1,R4>


Series: Create a Series from ndarray
• We passed the index values here. Now we can see the customized indexed values in
the output.
• Example:
#import the pandas library and aliasing as pd
import pandas as pd
import numpy as np
data = np.array(['a','b','c','d'])
s= pd.Series(data,index=[100,101,102,103])
print(s)
• Outcome:
100 a
101 b
102 c
103 d
dtype: object
<SELO: 1> <Reference No.: R1,R4>
Series: Create a Series from dict
• A dict can be passed as input and if no index is specified, then the dictionary keys
are taken in a sorted order to construct index. If index is passed, the values in data
corresponding to the labels in the index will be pulled out.
• Example:
#import the pandas library and aliasing as pd
import pandas as pd
import numpy as np
data = {'a' : 0., 'b' : 1., 'c' : 2.}
s= pd.Series(data)
print(s)
• Outcome:
a 0.0
b 1.0
c 2.0
dtype: float64
<SELO: 1> <Reference No.: R1,R4>
Series: Create a Series from dict
• Index order is persisted and the missing element is filled with NaN (Not a
Number).
• Example:
#import the pandas library and aliasing as pd
import pandas as pd
import numpy as np
data = {'a' : 0., 'b' : 1., 'c' : 2.}
s = pd.Series(data, index=['b', 'c', 'd', 'a'])
print(s)
• Outcome:
b 1.0
c 2.0
d NaN
a 0.0
dtype: float64
<SELO: 1> <Reference No.: R1,R4>
Series: Create a Series from Scalar
• If data is a scalar value, an index must be provided. The value will be
repeated to match the length of index
• Example:
#import the pandas library and aliasing as pd
import pandas as pd
import numpy as np
s = pd.Series(5, index=[0, 1, 2, 3])
print(s)
• Outcome:
0 5
1 5
2 5
3 5
dtype: int64
<SELO: 1> <Reference No.: R1,R4>
Series: Accessing Data from Series with Position
• Data in the series can be accessed similar to that in an ndarray.
• Example:
Retrieve the first element. As we already know, the counting starts from
zero for the array, which means the first element is stored at zeroth position
and so on.
import pandas as pd
s=pd.Series([1,2,3,4,5],index=['a','b','c','d','e'])
#retrieve the first element
print(s[0])
• Outcome:
1

<SELO: 1> <Reference No.: R1,R4>


Series: Accessing Data from Series with Position
• Example:
Retrieve the first three elements in the Series. If a : is inserted in front of it,
all items from that index onwards will be extracted. If two parameters (with
: between them) is used, items between the two indexes (not including the
stop index)
import pandas as pd
s=pd.Series([1,2,3,4,5],index=['a','b','c','d','e'])
#retrieve the first three element
print(s[:3])
• Outcome:
a 1
b 2
c 3
dtype: int64
<SELO: 1> <Reference No.: R1,R4>
Series: Retrieve Data Using Label (Index)
• A Series is like a fixed-size dict in that you can get and set
values by index label.
• Example: Retrieve a single element using index label value.
import pandas as pd
s=pd.Series([1,2,3,4,5],index=['a','b','c','d','e'])
#retrieve a single element
print(s['a'])
• Outcome:
1

<SELO: 1> <Reference No.: R1,R4>


Series: Retrieve Data Using Label (Index)
• Example: Retrieve multiple elements using a list of index label values.
import pandas as pd
s=pd.Series([1,2,3,4,5],index=['a','b','c','d','e'])
#retrieve multiple elements
print(s[['a','c','d']])
• Outcome:
a 1
c 3
d 4
dtype: int64

<SELO: 1> <Reference No.: R1,R4>


Series: Retrieve Data Using Label (Index)
• Example: If a label is not contained, an exception is raised.
import pandas as pd
s=pd.Series([1,2,3,4,5],index=['a','b','c','d','e'])
#retrieve multiple elements
print(s['f'])
• Outcome:
KeyError: 'f'

<SELO: 1> <Reference No.: R1,R4>


Learning Outcomes

The students have learn and understand the followings:

•Introduction
•Create an Empty Series
•Create a Series from ndarray
•Create a Series from dict
•Create a Series from Scalar
•Accessing Data from Series with Position
•Retrieve Data Using Label (Index)
References

1. Anaconda for python softwares(Jupiter notebook and spider IDE)


https://www.anaconda.com/products/individual
2. Python software for windows
https://www.python.org/downloads/
3. Online Google python notebook
https://colab.research.google.com/notebooks
Thank you

You might also like