Download Free PDF easy notes for Data handling using Pandas-I Series Class 12

In this article, Data handling using Pandas-I you will learn about Python Pandas data structure series.

Introduction to Python Libraries

Python libraries are in-built python modules that allow performing system-related operations, IO operations, data analysis and some other standard operations. Pandas library is used for data analysis.

Introduction to Data handling using Pandas-I

Pandas word derived from PANel Data System. It becomes popular for data analysis. It provides highly optimized performance with back end source code is purely written in C or Python. It makes a simple and easy process for data analysis. 

Pandas offers two basic data structures:

  1. Series
  2. DataFrame

To work with pandas import pandas library and create one object like this:

import pandas as pd

Looking for questions on series? Follow this link:

Assignment Questions on Pandas Series

Data handling using Pandas-I Series

Series is an important data structure of pandas. It represents one dimensional array, containing an array of data. It can any type of NumPy data. Basically series has two main components:

  1. An Array
  2. An index associated with array

Example:

Pandas Series Example
Pandas Series Example

Task 1 Creating Series

Series() function is used to create a series in Pandas. 

Example:

import pandas as pd
ser1=pd.Series()

An empty panda series has float64 data type.

Creating non-empty series

In non-empty series data and index will be supplied while creating series. Here data can be one of these data types:

  1. A python sequence
  2. An ndarray
  3. A dictionary
  4. A scalar value

Creating series with a python sequence (Data handling using Pandas-I)

Crating series with a Python sequence - Data handling using Pandas-I
Crating series with a Python sequence

Range function is used to generate a series with python pandas.

Series with float number Data handling using Pandas-I
Creating series with float numbers

In the above screenshot, a series is created with float numbers.

Creating Series with ndarray (Data handling using Pandas-I)

Creating Series with ndarray Data handling using Pandas-I
Creating Series with ndarray

Creating series from ndarray named nda. An array of an odd number between 1 to create through the range.

Creating series with dictionary

Creating series with dictionary - Data handling using Pandas-I
Creating series with dictionary

Crating series from Dictionary object and stored first three days of week in series.

Creating series with scalar value

Creating series with scalar value - Data handling using Pandas-I
Creating series with scalar value

Series created with scalar value 5.

Task 2 Specifying NaN values in the series

specifying NaN values in series
specifying NaN values in series

Specified NaN at the index 1.   

Task 3 creating series and specifying index

crating series and specifying index
crating series and specifying index

In the above example, two lists created for train numbers and train names. Train no list assigned as data and train name assigned as indexes.

Task 4 Creating series using arithmetic operation

Creating series using arithmetic operation
Creating series using arithmetic operation

In this example, series is created with a * 3 as data.

Data handling using Pandas-I Common Series attributes

AttributeDescription
Series.indexRetrieves index of a series
Series.valuesReturn series as ndarray
Series.dtypeReturn data type of series
Series.shapeReturn tuples (no.of rows) of the shape
Series.nbytesReturn no. of bytes
Series.ndimReturn no. of dimension
Series.sizeReturn no. of elements
Series.hasnansReturn true is there are any NaN value else false
Series.emptyReturn true if the series is empty, else false

Task 5 Common series attribute Example

series attributes example
series attributes example

Watch this video to understand practical aspects:

Task 6 Accessing elements from series

access series elements code
access series elements code

In above screenshot, I have accessed element by using its index value such as ser[2] and ser[3]. For accessing all the values using indexes you can use for loop.

Follow this link to read the questions and answer:

Informatics practices Series QnA

Task 7 Modifying series elements

modifying series elements python code
modifying series elements python code

In above code, I have changed the element value with a scalar value. In python, series objects are value mutable i.e. values can be changed but size immutable i.e. can’t be changed.

Task 8 Slicing in Series (Data handling using Pandas-I)

slicing in python pandas series data structures
slicing in python pandas series data structures

Task 9 head() and tail() function in series (Data handling using Pandas-I)

head functions in python pandas series
head functions in python pandas series

The head() function displays n number of elements from the top in the series. In the above example, I have accessed top 3 elements. If no value is passed in the parameter then by default it will display 5 elements from the top. Similarly, the tail function will work and display n number of elements from the bottom.

Task 10 Vector and arithmetic operations on series

vector and arithmetic operations on series
vector and arithmetic operations on series

Here I have used different vector operations and operators to perform various tasks on series.

Task 11 reindex() and drop() methods – (Data handling using Pandas-I)

reindex() : Create a similar object but with a different order of same indexes.

reindexing python pandas series (Data handling using Pandas-I)
reindexing python pandas series

drop(): Remove any entry from series.

drop elements from python pandas series - Data handling using Pandas-I
drop elements from python pandas series

Follow this link for practical programs with solution:

Python Pandas Series Practicals with solutions

Watch this video for series program:

Thank you for reading the article. Feel free to ask any doubt in the comment section and share this article with your friends and classmates.

Comment Your Views

%d bloggers like this: