statistical methods
- Use Python dictionary statistics
- Use Python's collection packet statistics class at Counter
- Value_counts use of pandas in the Python package of statistical category
1, using the dictionary dict to complete statistics
import random
a = [ random.randrange(1,9) for x in range(10)]
data = {}
for key in a:
data[key] = data.get(key, 0) + 1
print(data)
2, the use of collection bags Counter
from collections import Counter
import random
a = [ random.randrange(1,9) for x in range(10)]
result = Counter(a)
print(dict(result))
3, using the method in pandas packet value_counts
import pandas as pd
import random
a = [ random.randrange(1,9) for x in range(10)]
result = pd.value_counts(a)
print(result)
Note:利用pandas下的value_counts(),不仅可以统计list中各个元素出现的个数,还可对矩阵中的元素进行进行统计
.
For example:
import pandas as pd
a = pd.DataFrame([[1,2,3],
[3,1,3],
[1,2,1]])
result = a.apply(pd.value_counts)
print(result)