0% found this document useful (0 votes)
8 views

Python Pandas Series

The document provides an overview of data handling using the Pandas library in Python, focusing on the Series data structure, which is a one-dimensional labeled array capable of holding various data types. It explains how to create Series from different data types, access data using indexing methods like iloc and loc, and perform mathematical operations on Series. Additionally, it covers attributes of Series objects and methods for retrieving, filtering, and deleting elements.

Uploaded by

Akash Singh
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)
8 views

Python Pandas Series

The document provides an overview of data handling using the Pandas library in Python, focusing on the Series data structure, which is a one-dimensional labeled array capable of holding various data types. It explains how to create Series from different data types, access data using indexing methods like iloc and loc, and perform mathematical operations on Series. Additionally, it covers attributes of Series objects and methods for retrieving, filtering, and deleting elements.

Uploaded by

Akash Singh
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
You are on page 1/ 30

Data Handling

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

○ at the top of the python code/script file.


Some examples of Python Libraries-

● 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.

● Matplotlib library- It provides functions for plotting and drawing graphs.


Data Structure- Data structure is the arrangement of data in such a way
that permits efficient access and modification.

● Pandas Data Structures- Pandas offers the following data structures-

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:

● We can change the index in place also by


Series.index = [ ‘first’, ‘second’, ‘third’, ‘fourth’, ‘fifth’]
Creating a series using a list
● We can change the index in place also by
Series.index = [ ‘first’, ‘second’, ‘third’, ‘fourth’, ‘fifth’]
Accessing Data from a Series with
Position
● We can access data from a series by passing the position value and even
through slicing.
Accessing Data through iloc & loc
● Indexing and accessing can also be done using iloc and loc.
● iloc :- iloc is used for indexing and selecting based on position, i.e. by
row no. and column no. It refers to position-based indexing.
Syntax: iloc = [<row no. range>, <column no. range>]
● loc :- loc is used for indexing and selecting based on name, i.e. by row
name and column name. It refers to name-based indexing.
Syntax: iloc = [<list of row names>, <list of column names>]
Accessing Data using iloc & loc
Creating a Series from Scalar or Constant
Values
A series can be created using a scalar or constant value as shown
below. Here, data is a scalar value for which it is a must to provide
an index and the constant value shall be repeated to match the
length of the index.
Creating a Series from Scalar or Constant
Values
Alternatively, this can be done using range() method
Creating a Series with index of String type
String can be used as an index to the elements of a series.

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

Series.index Returns index of the series

Series.values Returns ndarray

Series.dtype Returns dtype object of the underlying data

Series.shape Returns tuple of the shape of underlying data

Series.nbytes Returns number of bytes of underlying data

Series.ndim Returns the number of dimension

Series.size Returns the number of elements

Series.itemsize Returns the size of the dtype

Series.hasnans Returns true if there are any NaN

Series.empty Returns true if series object is empty


Retrieving values from a series using head()
and tail() functions
The Series.head() function displays first ‘n’ from a pandas object. By
default, it gives us the top 5 rows of data in the series.
The Series.tail() function displays the last 5 elements by default.

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:

All these are


vector
operations.
Retrieving values using conditions
We can also give conditions to retrieve values from a series that satisfies the
given condition.
Example:

Here, it is performing the filter


operation and returns filtered
result containing only those
values that return True for the
given Boolean expression.
Deleting elements from a Series
We can delete an element from a series using drop( ) method by passing the
index of the element to be deleted as the argument to it.
Example:

Series will all Series after


the elements deleting item
intact. at index 3.

You might also like