Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Python Pandas - Slicing a Series Object



Pandas Series slicing is a process of selecting a group of elements from a Series object. A Series in Pandas is a one-dimensional labeled array that works similarly to the one-dimensional ndarray (NumPy array) but with labels, which are also called indexes.

Pandas Series slicing is very similarly to the Python and NumPy slicing but it comes with additional features, like slicing based on both position and labels. In this tutorial we will learn about the slicing operations on Pandas Series object.

Basics of Pandas Series Slicing

Series slicing can be done by using the [:] operator, which allows you to select subset of elements from the series object by specified start and end points.

Below are the syntax's of the slicing a Series −

  • Series[start:stop:step]: It selects elements from start to end with specified step value.

  • Series[start:stop]: It selects items from start to stop with step 1.

  • Series[start:]: It selects items from start to the rest of the object with step 1.

  • Series[:stop]: It selects the items from the beginning to stop with step 1.

  • Series[:]: It selects all elements from the series object.

Slicing a Series by Position

Pandas Series allows you to select the elements based on their position(i.e, Index values), just like Python list object.

Example: Slicing range of values from a Series

Following is the example of demonstrating how to slice a range value from a series object using the positions.

import pandas as pd
import numpy as np

data = np.array(['a', 'b', 'c', 'd'])
s = pd.Series(data)

# Display the Original series
print('Original Series:',s, sep='\n')

# Slice the range of values
result = s[1:3]

# Display the output
print('Values after slicing the Series:', result, sep='\n')

Following is the output of the above code −

Original Series:
0    a
1    b
2    c
3    d
dtype: object
Values after slicing the Series:
1    b
2    c
dtype: object

Example: Slicing the First Three Elements from a Series

This example retrieves the first three elements in the Series using it's position(i.e, index values).

import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])

#retrieve the first three element
print(s[:3])

Its output is as follows −

a  1
b  2
c  3
dtype: int64

Example: Slicing the Last Three Elements from a Series

Similar to the above example the following example retrieves the last three elements from the Series using the element position.

import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])

#retrieve the last three element
print(s[-3:])

Its output is as follows −

c  3
d  4
e  5
dtype: int64

Slicing a Series by Label

A Pandas Series is like a fixed-size Python dict in that you can get and set values by index labels.

Example: Slicing Group of elements from a Series using the Labels

The following example retrieves multiple elements with slicing the label of a Series.

import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])

# Slice multiple elements
print(s['a':'d'])

Its output is as follows −

a    1
b    2
c    3
d    4
dtype: int64

Example: Slicing First Three Elements using the Labels

The following example slice the first few elements using the label of a Series data.

import pandas as pd
s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])

# Slice multiple elements
print(s[:'c'])

Its output is as follows −

a    1
b    2
c    3
dtype: int64

Modifying Values after Slicing

After slicing a Series, you can also modify the values, by assigning the new values to those sliced elements.

Example

The following example demonstrates how to modify the series values after accessing the range values through slice.

import pandas as pd
s = pd.Series([1,2,3,4,5])

# Display the original series 
print("Original Series:\n",s)

# Modify the values of first two elements
s[:2] = [100, 200]

print("Series after modifying the first two elements:",s)

Following is the output of the above code −

Original Series:
 0    1
1    2
2    3
3    4
4    5
dtype: int64
Series after modifying the first two elements:
 0    100
1    200
2      3
3      4
4      5
dtype: int64
Advertisements