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

PythonPandasServer Side ProgrammingProgramming

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

To solve this, we will follow the steps given below −

  • Define a Series.

  • Create lambda filter method to extract the length of an integer value as follows,

len(pd.Series(filter(lambda x:type(x)==int,data)
  • Create lambda fliter method to extract length of float value as follows,

len(pd.Series(filter(lambda x:type(x)==float,data)
  • Create lambda fliter method to extract length of string value as follows,

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

Example

import pandas as pd
ls = [1,2,"python",3,4,5,6.5]
data = pd.Series(ls)
print("integer count:",len(pd.Series(filter(lambda x:type(x)==int,data))))
print("float count:",len(pd.Series(filter(lambda x:type(x)==float,data))))
print("string count:",len(pd.Series(filter(lambda x:type(x)==str,data))))

Output

integer count: 5
float count: 1
string count: 1
raja
Published on 10-Feb-2021 13:02:56
Advertisements