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

Understanding Pandas Series in Python

The document provides an overview of the pandas library in Python, focusing on its data structures, particularly the Series. It covers how to declare a Series, access its elements, assign values, and perform operations, including filtering and mathematical functions. Additionally, it discusses handling NaN values and operations between Series, highlighting the importance of meaningful indexing and data alignment.

Uploaded by

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

Understanding Pandas Series in Python

The document provides an overview of the pandas library in Python, focusing on its data structures, particularly the Series. It covers how to declare a Series, access its elements, assign values, and perform operations, including filtering and mathematical functions. Additionally, it discusses handling NaN values and operations between Series, highlighting the importance of meaningful indexing and data alignment.

Uploaded by

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

Python Programming

Dr. [Link]
Associate Professor – Department of Computer
Applications
Dr. N.G.P. ARTS AND SCIENCE COLLEGE
Dr. N.G.P.-KALAPATTI ROAD
COIMBATORE-641 048
Tamil Nadu, India
Mobile: +91 9841158445, E-mail: kavitha.k@[Link]

Dr.N.G.P. Arts and Science College 1


Coimbatore,Tamil Nadu, India
UNIT V - CONTENT TO BE DISCUSSED
The pandas Library
 Introduction to pandas Data Structure
• The Series
 Declaring a Series
 Selecting the Internal Elements
 Assigning values to the Elements
 Defining Series from NumPy Arrays & other Series
 Filtering Values
 Operations & Mathematical Functions
 Evaluating Values
 NaN Values
 Series as Dictionaries
2
 Operations between Series
Unit V – Pandas
Introduction to pandas Data Structure
Panda supports the following data structures
1. The Series – a sequence of one dimensional data
2. The DataFrame – a complex data structure with several
dimensions
3. The Index Objects – immutable objects that are responsible for
the labels on the axes and other metadata(like name of the axis)
1. The Series:
- An object of pandas library
- It consists of 2 one dimensional arrays
- Index array ( default values: 0,1,2,3)
- Value array ( contains data of NumPy type)
3
Declaring a Series:
>>> s = [Link]([12,-4,7,9])
>>> s
0 12
1 -4
2 7
3 9
dtype: int64

It is always preferable to create a Series using meaningful indexes.

>>> s = [Link]([12,-4,7,9], index=['a','b','c','d'])


>>> s
a 12
b -4
c 7
d 9 4
We can also print the 2 arrays separately.
>>> [Link]
array([12, -4, 7, 9], dtype=int64)
>>> [Link]
Index([u'a', u'b', u'c', u'd'], dtype='object')

Selecting or accessing the Internal Elements:


>>> s[2]
7
>>> s['b']
-4
>>> s[0:2]
a 12
b -4
dtype: int64
>>> s[['b','c']]
b -4
c 7
5
dtype: int64
Assigning Values to Elements:
>>> s[1] = 0
>>> s
a 12
b 0
c 7
d 9
dtype: int64
>>> s['b'] = 1
>>> s
a 12
b 1
c 7
d 9
dtype: int64 6
Defining Series from NumPy Arrays & Other Series:
- The values contained in the NumPy arrays (or) the original Series
are not copied, but they are passed by reference.
- So changes in the argument(NumPy or Series) will also be
reflected in the newly created Series object.
>>> arr = [Link]([1,2,3,4])
>>> s3 = [Link](arr)
>>> s3
0 1
1 2
2 3
3 4
dtype: int32 7
>>> s4 = [Link](s) >>> s3
>>> s4 0 1
a 12 1 2
b 4 2 3
c 7 3 4
d 9 dtype: int32
dtype: int64
>>> arr[2] = -2
>>> s3
0 1
1 2
2 -2
3 4
dtype: int32 8
Filtering Values:
- To know which element is > 8 in the series:
>>> s[s > 8]
a 12
d 9
dtype: int64

Operations & Mathematical Functions:


Like NumPy arrays: the operators (+,-,*,/) & Mathematical functions
are also applicable to object Series.

9
Using Operators:
>>> s / 2
a 6.0
b -2.0
c 3.5
d 4.5
dtype: float64
Using Mathematical function:
>>> [Link](s)
a 2.484907
b NaN
c 1.945910
d 2.197225
dtype: float64
10
Evaluating Values:
i) unique() function:
- A Series may contain duplicate values.
>>> serd = [Link]([1,0,2,1,2,3],
index=['white','white','blue','green','green','yellow'])
>>> serd
white 1
white 0
blue 2
green 1
green 2
yellow 3
dtype: int64
11
- To print the values excluding duplicate, we use the function
unique().
- The return value of this function will be an array containing the
unique values of the series.
>>> [Link]()
array([1, 0, 2, 3], dtype=int64)
ii) value_counts() function:
- It returns unique values.
- But calculates their occurrence within the series and prints them
>>> serd.value_counts()
2 2
1 2
3 1
0 1
dtype: int64 12
iii) isin() function:
- It is a membership evaluating function.
- To know whether some values are contained in the Series.
- If present it returns the boolean value ‘True’ else ‘False’.
>>> [Link]([0,3])
White False
White True
blue False
green False
green False
Yellow True
dtype: bool
>>> serd[[Link]([0,3])]
white 0
yellow 3
dtype: int64 13
NaN Values:
- NaN means – Not a Number
- Pandas supports NaN values for:
- Missing value
- Empty field
- Not definable numerical values
- Some times calculations of logarithms for negative values also
result in NaN values
- These NaN values are a problem during data analysis
- For missing values we can use [Link]
>>> s2 = [Link]([5,-3,[Link],14])
>>> s2
05
1 -3
2 NaN
14
3 14
- isnull() and notnull() functions are used to identify the indexes
without a value
>>> [Link]( )
0 False
1 False
2 True
3 False
dtype: bool
>>> [Link]( )
0 True
1 True
2 False
3 True
dtype: bool
15
- These two functions also used for filtering data.
>>> s2[[Link]( )]
0 5
1 -3
3 14
dtype: float64
>>> s2[[Link]( )]
2 NaN
dtype: float64

16
- These two functions also used for filtering data.
>>> s2[[Link]( )]
0 5
1 -3
3 14
dtype: float64
>>> s2[[Link]( )]
2 NaN
dtype: float64

17
>>> mydict = {'red': 2000, 'blue': 1000, 'yellow': 500, 'orange': 1000}
>>> myseries = [Link](mydict)
blue 1000
orange 1000
red 2000
yellow 500
dtype: int64
>>> colors = ['red','yellow','orange','blue','green']
>>> myseries = [Link](mydict, index=colors)
red 2000
yellow 500
orange 1000
blue 1000
green NaN
dtype: float64 18
Operations between Series:
- Series align data addressed differently between them by
identifying their corresponding label
>>> mydict2 = {'red':400,'yellow':1000,'black':700}
>>> myseries2 = [Link](mydict2)
>>> myseries + myseries2
black NaN
blue NaN
orange NaN
green NaN
red 2400
yellow 1500
dtype: float64
19
20

You might also like