In this pandas tutorial, we will discuss about Series and how to create Series , accessing elements from Series data structure.
Introduction
Series is an one dimensional data structure available in pandas module that stores elements of multiple data types.
We can create Series by using the following syntax.
pandas.Series(data)
where, data can be a list of elements.
Example:
In this example we will create a series for 10 elements.
#importing pandas module
import pandas
#create data that has 10 elements
data = ["R-language","php-mysql",1,2,3,4,5,6,7,8]
#create series from the above list
series_data = pandas.Series(data)
#dispay
print(series_data)
Output:
0 R-language
1 php-mysql
2 1
3 2
4 3
5 4
6 5
7 6
8 7
9 8
dtype: object
Accessing Elements
We can access particular elements in a Series through indexing. indexing starts with 0.
Syntax:
series_data[index_position]
where,
1. series_data is the input series
2. index_position is the element index to get accessed.
Example:
In this example, we will access different elements
#importing pandas module
import pandas
#create data that has 10 elements
data = ["R-language","php-mysql",1,2,3,4,5,6,7,8]
#create series from the above list
series_data = pandas.Series(data)
#get first element
print(series_data[0])
#get fifth element
print(series_data[4])
#get tenth element
print(series_data[9])
#get second element
print(series_data[1])
Output:
In this example, we will access first,fifth,tenth and second elements separately.
R-language
3
8
php-mysql
Creating Series from a Dictionary
We can create Series from a dictionary.
Example:
In this example, we will create a dictionary with 2 items and pass to series.
#importing pandas module
import pandas
#create dictionary with 2 elements
data = {1:'oops',2:'dbms'}
#create series from the above dictionary
series_data = pandas.Series(data)
#display
print(series_data)
Output:
1 oops
2 dbms
dtype: object
We can get the minimum,maximum and sum from the series using min(),max() and sum() functions.
Syntax:
min(seried_data)
max(seried_data)
sum(seried_data)
Example:
In this example, we will create seried with 10 integer elements and return minimum,maximum and total value.
#importing pandas module
import pandas
#create list with 10 elements
data = [34,56,768,65,4,4,56,78,99,43]
#create series from the above list
series_data = pandas.Series(data)
#get the minimum value
print(min(series_data))
#get the maximum value
print(max(series_data))
#get the total value
print(sum(series_data))
Output:
4
768
1207
We can get the series summary by using describe() method.
Syntax:
my_series.describe()
Example: In this example, we will get the summary of the series
#importing pandas module
import pandas
#create list with 10 elements
data = [34,56,768,65,4,4,56,78,99,43]
#create series from the above list
series_data = pandas.Series(data)
#get the summary
series_data.describe()
Output:
count 10.000000
mean 120.700000
std 229.385193
min 4.000000
25% 36.250000
50% 56.000000
75% 74.750000
max 768.000000
dtype: float64
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University.
Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :
Published Date :
Jun 14,2024