Write a program in Python to count the total number of integer, float and object data types in a given series

When working with Pandas Series, you may need to analyze the data types of elements. This program demonstrates how to count integer, float, and string data types in a given series using lambda functions and filters.

Problem Statement

Input ? Assume, you have a series,

0    1
1    2
2    python
3    3
4    4
5    5
6    6.5

Output ?

Total number of integer, float and string elements are,
integer count: 5
float count: 1
string count: 1

Solution Approach

To solve this problem, we will follow these steps ?

  • Define a Series with mixed data types.

  • Use lambda filter method to extract integer elements and count them:

len(pd.Series(filter(lambda x:type(x)==int,data)))
  • Use lambda filter method to extract float elements and count them:

len(pd.Series(filter(lambda x:type(x)==float,data)))
  • Use lambda filter method to extract string elements and count them:

len(pd.Series(filter(lambda x:type(x)==str,data)))

Complete Example

Here's the complete program to count different data types in a Pandas Series ?

import pandas as pd

# Create a series with mixed data types
data_list = [1, 2, "python", 3, 4, 5, 6.5]
data = pd.Series(data_list)

print("Series:")
print(data)
print("\nData type counts:")

# Count integers
integer_count = len(pd.Series(filter(lambda x: type(x) == int, data)))
print("integer count:", integer_count)

# Count floats  
float_count = len(pd.Series(filter(lambda x: type(x) == float, data)))
print("float count:", float_count)

# Count strings
string_count = len(pd.Series(filter(lambda x: type(x) == str, data)))
print("string count:", string_count)
Series:
0        1
1        2
2   python
3        3
4        4
5        5
6      6.5
dtype: object

Data type counts:
integer count: 5
float count: 1
string count: 1

Alternative Method Using value_counts()

You can also use Pandas built-in methods to analyze data types ?

import pandas as pd

data_list = [1, 2, "python", 3, 4, 5, 6.5]
data = pd.Series(data_list)

# Get data types of each element
data_types = data.apply(lambda x: type(x).__name__)
print("Data type distribution:")
print(data_types.value_counts())
Data type distribution:
int      5
str      1
float    1
Name: count, dtype: int64

Conclusion

Using lambda functions with filter() provides an effective way to count specific data types in a Pandas Series. The alternative method using apply() and value_counts() offers a more Pandas-native approach for the same task.

Updated on: 2026-03-25T15:49:24+05:30

794 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements