

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 filter the elements in a series which contains a string start and endswith ‘a’
Input − Assume, you have a Series,
0 apple 1 oranges 2 alpha 3 aroma 4 beta
Output − And, the result for elements start and endswith ‘a’.
2 alpha 3 aroma
Solution 1
Define a Series.
Create regular expression to check start and endswith ‘a’
r'^[a]$|^([a]).*\1$'
Create an empty list and set for loop and set if condition inside to check the pattern. It is defined below,
for i in data: if(re.search(exp, i)): ls.append(i)
Finally, check the series using isin().
Example
Let us see the following implementation to get a better understanding.
import pandas as pd import re l = ["apple","oranges","alpha","aroma","beta"] data = pd.Series(l) exp = r'^[a]$|^([a]).*\1$' ls = [] for i in data: if(re.search(exp, i)): ls.append(i) print(data[data.isin(ls)])
Output
2 alpha 3 aroma
Solution 2
Example
import pandas as pd import re l = ["apple","oranges","alpha","aroma","beta"] data = pd.Series(l) result = list(filter(lambda x:x.startswith('a') and x.endswith('a'),l)) print(data[data.isin(result)])
Output
2 alpha 3 aroma
- Related Questions & Answers
- Write a program in Python to filter only integer elements in a given series
- Write a program in Python to check if a series contains duplicate elements or not
- Write a program in Python to print the ‘A’ grade students’ names from a DataFrame
- 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 remove the elements in a series, if it contains exactly two spaces
- Write a Python program to shuffle all the elements in a given series
- Write a program in Python to round all the elements in a given series
- Write a program in Python to store the city and state names that start with ‘k’ in a given DataFrame into a new CSV file
- Write a program in Python to print the elements in a series between a specific range
- Python Program to Replace all Occurrences of ‘a’ with $ in a String
- Write a program in Python to find the maximum length of a string in a given Series
- Write a program in Python to print the power of all the elements 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
- Construct DFA of a string in which the second symbol from RHS is ‘a’
Advertisements