- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write a program in Python to slice substrings from each element in a given series
Assume, you have a series and the result for slicing substrings from each element in series as,
0 Ap 1 Oa 2 Mn 3 Kw
To solve this, we will follow the below approaches −
Solution 1
Define a series
Apply str.slice function inside start=0,stop-4 and step=2 to slice the substring from the series.
data.str.slice(start=0,stop=4,step=2)
Example
Let’s check the following code to get a better understanding −
import pandas as pd data = pd.Series(['Apple','Orange','Mango','Kiwis']) print(data.str.slice(start=0,stop=4,step=2))
Output
0 Ap 1 Oa 2 Mn 3 Kw
Solution 2
Define a series
Apply string index slice to start from 0 to end range as 4 and step value as 2. It is defined below,
data.str[0:4:2]
Example
Let’s check the following code to get a better understanding −
import pandas as pd data = pd.Series(['Apple','Orange','Mango','Kiwis']) print(data.str[0:4:2])
Output
0 Ap 1 Oa 2 Mn 3 Kw
- Related Articles
- Write a program in Python to filter valid dates in a given series
- Write a program in Python to filter armstrong numbers in a given series
- Write a program in Python to find the most repeated element in a series
- Write a program in Python to verify kth index element is either alphabet or number in a given series
- Write a program in Python to find the missing element in a given series and store the full elements in the same series
- Write a Python program to shuffle all the elements in a given series
- Write a Python program to find the maximum value from first four rows in a given series
- Write a program in Python to filter only integer elements in a given series
- Write a program in Python to round all the elements in a given series
- Golang program to remove an element from a slice
- Write a program in Python to print the most frequently repeated element in a series
- Write a program in Python to print the first and last three days from a given time series data
- Write a program in Python to find the index for NaN value in a given series
- Write a program in Python to find the maximum length of a string in a given Series
- Write a program in Python to sort all the elements in a given series in descending order

Advertisements