Sunday, 13 August 2023

Pandas Series: What is it and how to use it

Pandas series is designed to hold one column data of a table. In technical terms, a Series is a one-dimensional labelled array that can hold any type of data.

 

There are two primary components in a Series object.

a.   Data: Data hold be the series

b.   Index: Index is a label that uniquely identify the element in a series

 

Example 1: Get series from a list.

primes = [2, 3, 5, 7, 11]
primes_series = pd.Series(primes)

 

‘primes_series’ is a series object derived from list of primes and hold data like below.

0     2
1     3
2     5
3     7
4    11
dtype: int64

 

a.   0, 1, 2, 3, 4 specifies the index number to access the elements.

b.   dtype specifies the type of elements stored in the index.

 

How to access the elements of a series by index position?

You can access the elements of a Series using the index labels or index positions.

 

Access the elements by position based indexing

Position-based indexing (iloc) is useful when you want to access elements based on their numerical order, regardless of the specific index label.

# Access element by integer position
element_at_index_2 = primes_series.iloc[2]

 

Access the elements by label based indexing

Label-based indexing allows you to retrieve elements based on their assigned labels. You can supply the label in square brackets [label_name].

# Access element by label
element_with_label_2 = primes_series[2]

Since in this example, we derived the Series object from Python list, index label and index position both are same. We use iloc accessor to access the element by its index position, whereas index[] notation to access the element by it’s label.

 

Example 2: Get series from a dictionary.

primes_dictionary = {'first_prime' : 2, 'second_prime' : 3, 'third_prime' : 5}
primes_series = pd.Series(primes_dictionary)

‘primes_series’ is the series object that we constructed from a Python dictionary and hold below data.

 

first_prime     2

second_prime    3

third_prime     5

dtype: int64

 

a.   first_prime, second_prime and third_prime are the index labels used to access the elements of Series.

b.   dtype specifies the elements data type.

 

How to access the elements of a series by index position?

You can access the elements of a Series using the index labels or index positions.

# Access element by label
element_with_label_third_prime = primes_series['third_prime']

# Access element by integer position
element_at_index_2 = primes_series.iloc[2]

Example 3: Specify index labels explicitly for series derived from the list of elements.

primes = [2, 3, 5]
primes_index = ['first_prime', 'second_prime', 'third_prime']
primes_series = pd.Series(primes, index=primes_index)

‘primes_series’ points to below series data.

first_prime     2
second_prime    3
third_prime     5
dtype: int64

Now we can access the elements by index label or by its index position.

# Access element by label
element_with_label_third_prime = primes_series['third_prime']

# Access element by integer position
element_at_index_2 = primes_series.iloc[2]

Find the below working application.

        


hello_world.py 

import pandas as pd

# Example 1: Series from List
print('Series from List')
print('---------------------------')

# Get series from a list
primes = [2, 3, 5, 7, 11]
primes_series = pd.Series(primes)

print('primes_series')
print(primes_series)

# Access element by label
element_with_label_2 = primes_series[2]
print('\nelement_with_label_2 :', element_with_label_2)

# Access element by integer position
element_at_index_2 = primes_series.iloc[2]
print('\nelement_at_index_2 :', element_at_index_2)

# Example 2: Series from Dictionary
print('\nSeries from Dictionary')
print('---------------------------')

primes_dictionary = {'first_prime' : 2, 'second_prime' : 3, 'third_prime' : 5}
primes_series = pd.Series(primes_dictionary)
print('\nprimes_series')
print(primes_series)

# Access element by label
element_with_label_third_prime = primes_series['third_prime']
print('\nelement_with_label_third_prime :', element_with_label_third_prime)

# Access element by integer position
element_at_index_2 = primes_series.iloc[2]
print('\nelement_at_index_2 :', element_at_index_2)

# Example 3: Specify index labels explicitly for series derived from the list of elements.
print('\nSeries from List and explicit index')
print('---------------------------')
primes = [2, 3, 5]
primes_index = ['first_prime', 'second_prime', 'third_prime']
primes_series = pd.Series(primes, index=primes_index)
print('\nprimes_series')
print(primes_series)

# Access element by label
element_with_label_third_prime = primes_series['third_prime']
print('\nelement_with_label_third_prime :', element_with_label_third_prime)

# Access element by integer position
element_at_index_2 = primes_series.iloc[2]
print('\nelement_at_index_2 :', element_at_index_2)

 

Output

Series from List
---------------------------
primes_series
0     2
1     3
2     5
3     7
4    11
dtype: int64

element_with_label_2 : 5

element_at_index_2 : 5

Series from Dictionary
---------------------------

primes_series
first_prime     2
second_prime    3
third_prime     5
dtype: int64

element_with_label_third_prime : 5

element_at_index_2 : 5

Series from List and explicit index
---------------------------

primes_series
first_prime     2
second_prime    3
third_prime     5
dtype: int64

element_with_label_third_prime : 5

element_at_index_2 : 5

 

 

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment