
- Python Pandas - Home
- Python Pandas - Introduction
- Python Pandas - Environment Setup
- Python Pandas - Basics
- Python Pandas - Introduction to Data Structures
- Python Pandas - Index Objects
- Python Pandas - Panel
- Python Pandas - Basic Functionality
- Python Pandas - Indexing & Selecting Data
- Python Pandas - Series
- Python Pandas - Series
- Python Pandas - Slicing a Series Object
- Python Pandas - Attributes of a Series Object
- Python Pandas - Arithmetic Operations on Series Object
- Python Pandas - Converting Series to Other Objects
- Python Pandas - DataFrame
- Python Pandas - DataFrame
- Python Pandas - Accessing DataFrame
- Python Pandas - Slicing a DataFrame Object
- Python Pandas - Modifying DataFrame
- Python Pandas - Removing Rows from a DataFrame
- Python Pandas - Arithmetic Operations on DataFrame
- Python Pandas - IO Tools
- Python Pandas - IO Tools
- Python Pandas - Working with CSV Format
- Python Pandas - Reading & Writing JSON Files
- Python Pandas - Reading Data from an Excel File
- Python Pandas - Writing Data to Excel Files
- Python Pandas - Working with HTML Data
- Python Pandas - Clipboard
- Python Pandas - Working with HDF5 Format
- Python Pandas - Comparison with SQL
- Python Pandas - Data Handling
- Python Pandas - Sorting
- Python Pandas - Reindexing
- Python Pandas - Iteration
- Python Pandas - Concatenation
- Python Pandas - Statistical Functions
- Python Pandas - Descriptive Statistics
- Python Pandas - Working with Text Data
- Python Pandas - Function Application
- Python Pandas - Options & Customization
- Python Pandas - Window Functions
- Python Pandas - Aggregations
- Python Pandas - Merging/Joining
- Python Pandas - MultiIndex
- Python Pandas - Basics of MultiIndex
- Python Pandas - Indexing with MultiIndex
- Python Pandas - Advanced Reindexing with MultiIndex
- Python Pandas - Renaming MultiIndex Labels
- Python Pandas - Sorting a MultiIndex
- Python Pandas - Binary Operations
- Python Pandas - Binary Comparison Operations
- Python Pandas - Boolean Indexing
- Python Pandas - Boolean Masking
- Python Pandas - Data Reshaping & Pivoting
- Python Pandas - Pivoting
- Python Pandas - Stacking & Unstacking
- Python Pandas - Melting
- Python Pandas - Computing Dummy Variables
- Python Pandas - Categorical Data
- Python Pandas - Categorical Data
- Python Pandas - Ordering & Sorting Categorical Data
- Python Pandas - Comparing Categorical Data
- Python Pandas - Handling Missing Data
- Python Pandas - Missing Data
- Python Pandas - Filling Missing Data
- Python Pandas - Interpolation of Missing Values
- Python Pandas - Dropping Missing Data
- Python Pandas - Calculations with Missing Data
- Python Pandas - Handling Duplicates
- Python Pandas - Duplicated Data
- Python Pandas - Counting & Retrieving Unique Elements
- Python Pandas - Duplicated Labels
- Python Pandas - Grouping & Aggregation
- Python Pandas - GroupBy
- Python Pandas - Time-series Data
- Python Pandas - Date Functionality
- Python Pandas - Timedelta
- Python Pandas - Sparse Data Structures
- Python Pandas - Sparse Data
- Python Pandas - Visualization
- Python Pandas - Visualization
- Python Pandas - Additional Concepts
- Python Pandas - Caveats & Gotchas
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