Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Write a program in Python to filter the elements in a series which contains a string start and endswith 'a'
In this tutorial, we'll learn how to filter elements in a Pandas Series that start and end with the same character. We'll explore different approaches to find strings that begin and end with 'a'.
Sample Input and Output
Input ? Assume, you have a Series:
0 apple 1 oranges 2 alpha 3 aroma 4 beta
Output ? The result for elements that start and end with 'a':
2 alpha 3 aroma
Using Regular Expression
The first approach uses regular expressions to match strings that start and end with 'a'. The pattern r'^[a]$|^([a]).*\1$' matches single 'a' or strings starting and ending with 'a'.
Example
import pandas as pd
import re
fruits = ["apple", "oranges", "alpha", "aroma", "beta"]
data = pd.Series(fruits)
# Regular expression pattern
exp = r'^[a]$|^([a]).*\1$'
matched_items = []
for item in data:
if re.search(exp, item):
matched_items.append(item)
result = data[data.isin(matched_items)]
print(result)
2 alpha 3 aroma dtype: object
Using String Methods with Lambda
A simpler approach uses Python's built-in string methods startswith() and endswith() with the filter() function.
Example
import pandas as pd
fruits = ["apple", "oranges", "alpha", "aroma", "beta"]
data = pd.Series(fruits)
# Filter using lambda function
filtered_items = list(filter(lambda x: x.startswith('a') and x.endswith('a'), fruits))
result = data[data.isin(filtered_items)]
print(result)
2 alpha 3 aroma dtype: object
Using Pandas String Accessor
The most Pythonic approach uses Pandas' built-in string accessor methods for efficient filtering.
Example
import pandas as pd
fruits = ["apple", "oranges", "alpha", "aroma", "beta"]
data = pd.Series(fruits)
# Using Pandas string methods
result = data[data.str.startswith('a') & data.str.endswith('a')]
print(result)
2 alpha 3 aroma dtype: object
Comparison
| Method | Complexity | Performance | Readability |
|---|---|---|---|
| Regular Expression | High | Medium | Low |
| Lambda with filter() | Medium | Good | Medium |
| Pandas String Accessor | Low | Best | High |
Conclusion
The Pandas string accessor method (data.str.startswith() & data.str.endswith()) is the most efficient and readable approach. It's specifically designed for Series operations and offers better performance than regular expressions or lambda functions.
