In this article, Data Handling using Pandas-I you will learn about the Python Pandas data structure series.
Topics Covered
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:
- Series
- 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:
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:
- An Array (Values)
- An index associated with an array (labels)
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
![Download Free PDF easy notes for Data handling using Pandas-I Series Class 12 1 creating pandas series 6 methods](https://i0.wp.com/www.tutorialaicsip.com/wp-content/uploads/2023/04/image-25.png?resize=436%2C530&ssl=1)
In non-empty series data and index will be supplied while creating series. Here data can be one of these data types:
- A python sequence
- An ndarray
- A dictionary
- A scalar value
- Equi-space Elements
- Repeated list
Creating series with a python sequence (Data handling using Pandas-I)
range() function is used to generate a series with python pandas.
import pandas as pd
s=pd.Series(range(5))
print(s)
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:
![Download Free PDF easy notes for Data handling using Pandas-I Series Class 12 2 creating pandas series of vowels](https://i0.wp.com/www.tutorialaicsip.com/wp-content/uploads/2023/04/image-23.png?resize=150%2C123&ssl=1)
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 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:
![Download Free PDF easy notes for Data handling using Pandas-I Series Class 12 3 creating series with decimal step values in python pandas](https://i0.wp.com/www.tutorialaicsip.com/wp-content/uploads/2023/04/image-26.png?resize=196%2C265&ssl=1)
Creating a series with a dictionary
![Creating series with dictionary Creating series with dictionary - Data handling using Pandas-I](https://i0.wp.com/www.tutorialaicsip.com/wp-content/uploads/2020/09/creating-series-with-dictionary.jpg?resize=390%2C138&ssl=1)
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
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](https://i0.wp.com/www.tutorialaicsip.com/wp-content/uploads/2020/09/specifying-NaN-values-in-series.jpg?resize=370%2C106&ssl=1)
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:
![Download Free PDF easy notes for Data handling using Pandas-I Series Class 12 4 specifying NaN values in python pandas series](https://i0.wp.com/www.tutorialaicsip.com/wp-content/uploads/2023/04/image-27.png?resize=215%2C189&ssl=1)
Task 3 creating series and specifying index
![crating series and specifying index crating series and specifying index](https://i0.wp.com/www.tutorialaicsip.com/wp-content/uploads/2020/09/crating-series-and-specifying-index.jpg?resize=480%2C144&ssl=1)
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
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:
![Download Free PDF easy notes for Data handling using Pandas-I Series Class 12 5 create a series in Python pandas using equispaced elements](https://i0.wp.com/www.tutorialaicsip.com/wp-content/uploads/2023/04/image-28.png?resize=222%2C166&ssl=1)
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:
- Using tile() function of numpy module
- 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)
![Download Free PDF easy notes for Data handling using Pandas-I Series Class 12 6 creating a series by repeating list](https://i0.wp.com/www.tutorialaicsip.com/wp-content/uploads/2023/04/image-29.png?resize=190%2C265&ssl=1)
Data handling using Pandas-I Common Series attributes
Attribute | Description |
Series.index | Retrieves index of a series |
Series.values | Return series as ndarray |
Series.dtype | Return data type of series |
Series.shape | Return tuples (no.of rows) of the shape |
Series.nbytes | Return no. of bytes |
Series.ndim | Return no. of dimension |
Series.size | Return no. of elements |
Series.hasnans | Return true is there are any NaN value else false |
Series.empty | Return true if the series is empty, else false |
Common series attribute Example
![series attributes example series attributes example](https://i0.wp.com/www.tutorialaicsip.com/wp-content/uploads/2020/09/series-attributes-example.jpg?resize=480%2C251&ssl=1)
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:
![Download Free PDF easy notes for Data handling using Pandas-I Series Class 12 7 Pandas Series attributes program output](https://i0.wp.com/www.tutorialaicsip.com/wp-content/uploads/2023/04/image-30.png?resize=640%2C157&ssl=1)
Watch this video to understand the practical aspects:
Accessing elements from series
![access series elements python code access series elements code](https://i0.wp.com/www.tutorialaicsip.com/wp-content/uploads/2020/09/accesse-series-elements-code.jpg?resize=474%2C174&ssl=1)
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 8 Download Free PDF easy notes for Data handling using Pandas-I Series Class 12](https://i0.wp.com/www.tutorialaicsip.com/wp-content/uploads/2023/04/image-31.png?resize=323%2C76&ssl=1)
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 9 Download Free PDF easy notes for Data handling using Pandas-I Series Class 12](https://i0.wp.com/www.tutorialaicsip.com/wp-content/uploads/2023/04/image-32.png?resize=242%2C57&ssl=1)
Follow this link to read the questions and answer:
Modifying series elements
![modifying series elements python code modifying series elements python code](https://i0.wp.com/www.tutorialaicsip.com/wp-content/uploads/2020/09/modifying-series-elements-python-code.jpg?resize=512%2C157&ssl=1)
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](https://i0.wp.com/www.tutorialaicsip.com/wp-content/uploads/2020/09/slicing-in-python-pandas-series-data-structures.jpg?resize=511%2C331&ssl=1)
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](https://i0.wp.com/www.tutorialaicsip.com/wp-content/uploads/2020/09/head-functions-in-python-pandas-series.jpg?resize=400%2C257&ssl=1)
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 10 Download Free PDF easy notes for Data handling using Pandas-I Series Class 12](https://i0.wp.com/www.tutorialaicsip.com/wp-content/uploads/2023/04/image-33.png?resize=251%2C165&ssl=1)
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 11 Download Free PDF easy notes for Data handling using Pandas-I Series Class 12](https://i0.wp.com/www.tutorialaicsip.com/wp-content/uploads/2023/04/image-34.png?resize=228%2C160&ssl=1)
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 12 Download Free PDF easy notes for Data handling using Pandas-I Series Class 12](https://i0.wp.com/www.tutorialaicsip.com/wp-content/uploads/2023/04/image-36.png?resize=253%2C117&ssl=1)
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 13 Download Free PDF easy notes for Data handling using Pandas-I Series Class 12](https://i0.wp.com/www.tutorialaicsip.com/wp-content/uploads/2023/04/image-35.png?resize=237%2C109&ssl=1)
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 14 Download Free PDF easy notes for Data handling using Pandas-I Series Class 12](https://i0.wp.com/www.tutorialaicsip.com/wp-content/uploads/2023/04/image-39.png?resize=242%2C265&ssl=1)
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 drop elements from python pandas series - Data handling using Pandas-I](https://i0.wp.com/www.tutorialaicsip.com/wp-content/uploads/2020/09/drop-elements-from-python-pandas-series.jpg?resize=412%2C145&ssl=1)
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:
![Download Free PDF easy notes for Data handling using Pandas-I Series Class 12 15 drop methohd with pandas series](https://i0.wp.com/www.tutorialaicsip.com/wp-content/uploads/2023/04/image-40.png?resize=238%2C110&ssl=1)
Follow this link for practical programs with solutions:
Watch this video for series program:
Follow this link to read questions about the Python pandas series for class 12.
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.