Đầu vào - Giả sử, bạn có một chuỗi,
0 1 1 2 2 python 3 3 4 4 5 5 6 6.5
Đầu ra -
Total number of integer, float and string elements are, integer count: 5 float count: 1 string count: 1
Giải pháp
Để giải quyết vấn đề này, chúng tôi sẽ làm theo các bước dưới đây -
-
Xác định một chuỗi.
-
Tạo phương thức lọc lambda để trích xuất độ dài của một giá trị số nguyên như sau,
len(pd.Series(filter(lambda x:type(x)==int,data)
-
Tạo phương thức lambda fliter để trích xuất độ dài của giá trị float như sau,
len(pd.Series(filter(lambda x:type(x)==float,data)
-
Tạo phương thức lambda fliter để trích xuất độ dài của giá trị chuỗi như sau,
len(pd.Series(filter(lambda x:type(x)==str,data)
Ví dụ
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))))
Đầu ra
integer count: 5 float count: 1 string count: 1