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 the 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

Important points for pandas:

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

Pandas offer two basic data structures:

  1. Series
  2. DataFrame

To work with pandas import the pandas library and create one object using one of these statements:

import pandas
import pandas as pd
from pandas import Series, DataFrame

Looking for questions on the 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 a one-dimensional array, containing an array of data. It can be any type of NumPy data. Basically series has two main components:

  1. An Array (Values)
  2. An index associated with an array (labels)

Example:

Pandas Series Example
Pandas Series Example

Task 1 Creating Series

The Series() function from the pandas module is used to create a series. 

Example:

import pandas as pd
ser1=pd.Series()

An empty panda series has float64 data type.

Creating non-empty series

creating pandas series 6 methods

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
  5. Equi-space Elements
  6. Repeated list

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.

import pandas as pd
s=pd.Series(range(5))
print(s)
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. Observe this code:

import pandas as pd
s=pd.Series([3.5,6.5,7.5,4.5,8.0])
print(s)

Series can be also created using a list of characters. Observe this series of vowels.

import pandas as pd
s=pd.Series(['a','e','i','o','u'])
print(s)

Output:

creating pandas series of vowels

A series can be also created using a list of words/names. Let’s have a look at this code:

import pandas as pd
s=pd.Series(['alpha','beta','gama'])
print(s)

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

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

Creating a series from ndarray named nda. An array of odd numbers between 1 to 10 is created through the arange() function.

import pandas as pd
import numpy as np
nda=np.arrange(1,10,2)
s=pd.Series(nda)
print(s)

The arange() function also useful for creating a series with decimal step values such as 0.5, 0.8 etc. Observe this code:

import pandas as pd
import numpy as np
s=pd.Series(np.arange(0.5,5.5,0.5))
print(s)

Output:

creating series with decimal step values in python pandas

Creating a series with a dictionary

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

Crating series from Dictionary object and storing first three days of the week in series. when a series is created using a dictionary, the dictionary keys become an index of a series by default.

import pandas as pd
d={'Monday':1,'Tuesday':2,'Wednesday':3}
s=pd.Series(d)
print(s)

Creating a series with a scalar value

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

Series created with scalar value 5. When a series is created using a scalar value, the index must be specified.

import pandas as pd
s=pd.Series(5,index=range(1,5))
print(s)

Task 2 Specifying NaN values in the series

specifying NaN values in series
specifying NaN values in series

Specified NaN at the index 1. If the data value for the series is unknown, python assigns the value as NaN (Not a Number). There are two methods to assign NaN: np.NaN and None in the sequence.

import pandas as pd
import numpy as np
#Method1
s=pd.Series([1,np.NaN,4,np.NaN,8,9])
#Method 2
s=pd.Series([1,None,4,None,8,9])
print(s)

Output:

specifying NaN values in python pandas series

Task 3 creating series and specifying index

crating series and specifying index
creating series and specifying index

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

import pandas as pd
tr_no=[19708,14708,19115,14155]
tr_name=['Aravali Express','Ranakpur Express','Sayaji Nagari Express','Kutch Express']
s=pd.Series(data=tr_no,index=tr_name)
print(s)

Task 4 Creating series using arithmetic operation

Creating series using arithmetic operation
Creating series using arithmetic operation

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

import pandas as pd
import numpy as np
l=np.arange(25,50,5)
s=pd.Series(index=l,data=l*3)
print(s)

Task 5 Creating series with equispaced elements

To create a series with equispaced elements linspace() function of numpy module is used. Let’s have a look:

import pandas as pd
import numpy as np
s=pd.Series(np.linspace(31,91,5))
print(s)

Output:

create a series in Python pandas using equispaced elements

Task 6 Creating series using the repeated list

A list can be replicated or repeated to create a series. This can be done using two methods:

  1. Using tile() function of numpy module
  2. using replication operator for list

Just have a look at the following code:

Method 1

import pandas as pd
import numpy as np
s=pd.Series(np.tile([33,44,55],3))
print(s)

Method 2

import pandas as pd
l=[33,44,55]
s=pd.Series(l*3)
print(s)
creating a series by repeating list

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

Common series attribute Example

series attributes example
series attributes example

Attributes Example:

import pandas as pd
d={'Jan':133,'Feb':145,'Mar':165,'Apr':126,'May':176}
s=pd.Series(d)
print("Index:",s.index)
print("Values:",s.values)
print("Shape:",s.shape)
print("Bytes:",s.nbytes)
print("Dimension:",s.ndim)
print("Size:",s.size)
print("Contains NaN Items?:",s.hasnans)

Output:

Pandas Series attributes program output

Watch this video to understand the practical aspects:

Accessing elements from series

access series elements code
access series elements code

In the above screenshot, I have accessed elements by using their index value such as ser[2] and ser[3].

Observe the following which accesses 3rd and last element using a positional index.

import pandas as pd
d={'Jan':133,'Feb':145,'Mar':165,'Apr':126,'May':176}
s=pd.Series(d)
print("Element 3:",s[2])
print("Last Element:",s[-1])

Output:

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

The series can be also accessed using its label or index. Observe this code:

import pandas as pd
d={'Jan':133,'Feb':145,'Mar':165,'Apr':126,'May':176}
s=pd.Series(d)
print("January:",s['Jan'])
print("April:",s['Apr'])
Download Free PDF easy notes for Data handling using Pandas-I Series Class 12

Follow this link to read the questions and answer:

Informatics practices Series QnA

Modifying series elements

modifying series elements python code
modifying series elements python code

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

Example:

import pandas as pd
s=pd.Series([34,56,78,21,90])
s[3]=23
print(s)

Slicing in Series (Data handling using Pandas-I)

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

Slicing is also one of the methods to select/access or modify data from a series. Observe this code:

import pandas as pd
s=pd.Series([34,56,78,21,90])
print(s[1:])
print(s[2:5])
print(s[0::2])
s[0:4]=[23,32,87,19]
print(s)

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.

Observe this code and the output will return the first 5 elements from series:

import pandas as pd
s=pd.Series([34,56,78,21,90,88,58,95,97])
print(s.head())

Output:

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

The following code will return the last 5 elements from a series:

import pandas as pd
s=pd.Series([34,56,78,21,90,88,58,95,97])
print(s.tail())
Download Free PDF easy notes for Data handling using Pandas-I Series Class 12

The following codes display n rows from top and bottom, I have taken 3 as n.

import pandas as pd
s=pd.Series([34,56,78,21,90,88,58,95,97])
print(s.head(3))
Download Free PDF easy notes for Data handling using Pandas-I Series Class 12
import pandas as pd
s=pd.Series([34,56,78,21,90,88,58,95,97])
print(s.tail(3))
Download Free PDF easy notes for Data handling using Pandas-I Series Class 12

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.

Observe this code:

import pandas as pd
s=pd.Series([34,56,78,21,90])
print("Add 5 to each value:")
print(s+5)
print("Display value more than 80:")
print(s>80)
print("Display values less than 70")
print(s<70)
Download Free PDF easy notes for Data handling using Pandas-I Series Class 12

Watch this video for practical understanding:

drop() method – (Data handling using Pandas-I)

drop(): Remove any entry from the series.

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

Observe this code and output to understand:

import pandas as pd
s=pd.Series([34,56,78,21,90])
print("Add 5 to each value:")
s=s.drop(2)
print(s)

Output:

drop methohd with pandas series

Follow this link for practical programs with solutions:

Python Pandas Series Practicals with solutions

Watch this video for series program:

Follow this link to read questions about the Python pandas series for class 12.

Important Questions for Python pandas series class XII

Download PDF

Download pdf for Python Pandas – Series from the below given button:

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.

Leave a Reply