- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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 count the total number of integer, float and object data types in a given series
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
Advertisements