Python Pandas Series
Python Pandas Series
Using Pandas
Python Pandas
Series
● Python module- A python module is a python script file(.py file) containing variables, python
classes, functions, statements etc.
● Python Library/package- A Python library is a collection of modules that together cater to a
specific type of need or application. The advantage of using libraries is that we can directly use
functions/methods for performing specific type of application instead of rewriting the code for
that particular use. They are used by
○ using the import command as- import libraryname
● Python standard library-It is a collection of library which is normally distributed along with
Python installation. Some of them are:
1. math module- provides mathematical functions.
2. random module- provides functions for generating pseudo-random numbers.
3. statistics module- provides statistical functions.
● Numpy (Numerical Python) library- It provides functions for working with large multi-
dimensional arrays(ndarrays) and matrices. NumPy provides a large set of mathematical
functions that can operate quickly on the entries of the ndarray without the need of loops.
● Pandas (PANel + DAta) library- Pandas is a fast, powerful, flexible and easy to use open source
data analysis and manipulation tool. Pandas is built on top of NumPy, relying on ndarray and its
fast and efficient array based mathematical functions.
a) Series - 1D array
b) DataFrame - 2D array
c) Panel - 3D array (not in syllabus)
Series
Series is a one-dimensional array like structure with homogeneous data. For
example, the following series is a collection of integers 10, 23, 56, …
Key Points :
● Homogeneous data
● Size Immutable
● Values of Data Mutable
Series (contd.)
● Pandas 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.
● Pandas Series is nothing but a column in an excel sheet.
● Labels need not be unique but must be a hashable type.
● The object supports both integer and
label-based indexing and provides
a host of methods for performing
operations involving the index.
Creating a series
Output:
Series() with arguments
Syntax:
<Series Object> = pandas.Series(data, index = idx, [dtype =
<data type>])
The data supplied to Series() can be either:
● A Sequence (list)
● An ndarray
● A scalar value
● A python dictionary
● A mathematical expression/function
Creating a series using a list
Since, a list is also a one-dimensional data type, it can be converted into a
series using Series() method.
Code: Output:
Creating a series using a list
● To create a series using range() method.
Code:
Output:
IN
OUT
Creating a Series with range() & for loop
IN
OUT
Creating a Series using two different lists
The two lists are passed as arguments to Series() method, out of
which the first list will be index and the other one will be the value.
IN
OUT
Creating a Series using missing values
(NaN)
In certain situations, we need to create a series object for which size is
defined but some elements or data are missing. This is handled by defining
NaN (Not a number) value(s), which is an attribute of Numpy library and this
can be achieved by defining a missing value using np.NaN.
IN
OUT
Creating a Series using Dictionary
Using dictionary for creating a series gives us the advantage of built-in keys
used as index. We don’t require declaring an index as a separate list; instead,
built-in keys will be treated as the index.
IN
OUT
Creating a Series using mathematical
expression/function
A series object can be created by defining a function or a mathematical
expression that determines the values for data sequence using the syntax as
follows:
<Series Object> = pd.Series (index = None, data = <expression [function]>)
IN
OUT
Creating a Series using a mathematical
function
A series using a mathematical exponentiation function.
IN
OUT
Series Object Attributes
Some common attributes related to series object are described below and are
accessed using the syntax: <series object>.<Attributename>
Attribute Description
IN
OUT
Mathematical Operations on a series
Mathematical processing can be performed on series using scalar values and
functions. All the arithmetic operators such as +, -, *, /, etc. can be
successfully performed on series.
Example:
Note:
Arithmetic operation
is possible on objects
of same index;
otherwise will result
as NaN.
Vector Operations on a series
Series also supports vector operations. Any operation to be performed on a
series gets performed on every single element of it.
Example: