Trong chương trình này, chúng ta sẽ tính tần suất của mỗi phần tử trong Pandas series. Hàm value_counts() trong pandas giúp tìm tần suất.
Thuật toán
Bước 1: Định nghĩa Pandas series. Bước 2: In tần suất mỗi phần tử bằng value_counts().
Mã ví dụ
import pandas as pd
series = pd.Series([10, 10, 20, 30, 40, 30, 50, 10, 60, 50, 50])
print("Series:\n", series)
frequency = series.value_counts()
print("\nTần suất các phần tử:\n", frequency)
Kết quả
Series: 0 10 1 10 2 20 3 30 4 40 5 30 6 50 7 10 8 60 9 50 10 50 dtype: int64 Tần suất các phần tử: 50 3 10 3 30 2 20 1 40 1 60 1 dtype: int64