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 only integer elements in a given series
When working with Pandas Series that contain mixed data types, you often need to filter only the integer elements. Python provides several approaches to accomplish this task using type checking, regular expressions, or built-in Pandas methods.
Sample Data
Let's start by creating a series with mixed data types ?
import pandas as pd
data_list = [1, 2, "python", "pandas", 3, 4, 5]
series = pd.Series(data_list)
print("Original Series:")
print(series)
Original Series: 0 1 1 2 2 python 3 pandas 4 3 5 4 6 5 dtype: object
Method 1: Using Type Checking
The most straightforward approach is to check the type of each element using Python's built-in type() function ?
import pandas as pd
data_list = [1, 2, "python", "pandas", 3, 4, 5]
series = pd.Series(data_list)
print("Integer elements only:")
for index, value in series.items():
if type(value) == int:
print(index, value)
Integer elements only: 0 1 1 2 4 3 5 4 6 5
Method 2: Using Regular Expressions
You can use regular expressions with the filter() function to identify numeric strings and combine with isin() to filter the original series ?
import pandas as pd
import re
data_list = [1, 2, "python", "pandas", 3, 4, 5]
series = pd.Series(data_list)
# Filter using regex pattern for digits
filtered_values = pd.Series(filter(lambda x: re.match(r"\d+", str(x)), series))
result = series[series.isin(filtered_values)]
print("Filtered integer elements:")
print(result)
Filtered integer elements: 0 1 1 2 4 3 5 4 6 5 dtype: object
Method 3: Using Boolean Indexing
A more Pandas-native approach uses boolean indexing to filter elements ?
import pandas as pd
data_list = [1, 2, "python", "pandas", 3, 4, 5]
series = pd.Series(data_list)
# Create boolean mask for integer elements
integer_mask = series.apply(lambda x: isinstance(x, int))
filtered_series = series[integer_mask]
print("Using boolean indexing:")
print(filtered_series)
Using boolean indexing: 0 1 1 2 4 3 5 4 6 5 dtype: object
Comparison
| Method | Best For | Performance |
|---|---|---|
| Type Checking | Simple iteration with custom logic | Good |
| Regular Expressions | String pattern matching | Slower |
| Boolean Indexing | Pandas-native operations | Best |
Conclusion
Use boolean indexing with isinstance() for the most efficient Pandas-native filtering. Type checking with iteration works well for custom processing, while regex is useful when dealing with string representations of numbers.
