计算每个类别的数量在数据分析中是非常常见的,本文将从多个方面进行阐述。
一、按照类别列进行分类汇总
import pandas as pd
# 创建示例数据
data = {'category': ['A', 'A', 'B', 'C', 'C', 'C'], 'value': [1, 2, 3, 4, 5, 6]}
df = pd.DataFrame(data)
# 按照 category 列进行分类汇总
result = df.groupby('category').size().reset_index(name='counts')
print(result)
首先,我们可以通过 pandas 中的 groupby() 函数将数据按照类别列进行分组,再利用 size() 函数统计每个组的数据量,并通过 reset_index() 函数将结果转化成 DataFrame 格式。上述代码运行结果如下:
category counts 0 A 2 1 B 1 2 C 3
二、使用 Counter 类进行计数
from collections import Counter # 创建示例数据 data = ['A', 'A', 'B', 'C', 'C', 'C'] # 使用 Counter 类实现计数 result = Counter(data) print(result)
Counter 类可以方便地实现对一个列表中元素的计数,返回一个字典类型的计数结果。上述代码运行结果如下:
Counter({'C': 3, 'A': 2, 'B': 1})
三、使用 dict 和循环进行计数
# 创建示例数据
data = ['A', 'A', 'B', 'C', 'C', 'C']
# 使用 dict 和循环实现计数
result = {}
for d in data:
if d not in result:
result[d] = 1
else:
result[d] += 1
print(result)
我们也可以通过创建一个空字典,并使用循环对每个元素进行判断、计数来实现统计。上述代码运行结果如下:
{'A': 2, 'B': 1, 'C': 3}
四、使用 SQL 语句进行分组计数
import sqlite3
# 创建示例数据
data = [('A', 1), ('A', 2), ('B', 3), ('C', 4), ('C', 5), ('C', 6)]
# 将数据插入 SQLite 数据库中
conn = sqlite3.connect(':memory:')
c = conn.cursor()
c.execute('''CREATE TABLE data (category text, value int)''')
c.executemany('INSERT INTO data VALUES (?,?)', data)
# 使用 SQL 语句实现按照 category 列分组统计
c.execute('''SELECT category, COUNT(category) FROM data GROUP BY category''')
result = c.fetchall()
print(result)
如果数据存储在 SQLite 数据库中,我们可以通过 SQL 语句中的 GROUP BY 子句实现按照类别列分组、计数。上述代码运行结果如下:
[('A', 2), ('B', 1), ('C', 3)]
五、使用 map 和 lambda 函数实现计数
# 创建示例数据 data = ['A', 'A', 'B', 'C', 'C', 'C'] # 使用 map 和 lambda 函数实现计数 result = dict(map(lambda x: (x, data.count(x)), data)) print(result)
最后,我们也可以使用 map 和 lambda 函数来实现计数。上述代码运行结果如下:
{'A': 2, 'B': 1, 'C': 3}
原创文章,作者:NOPVZ,如若转载,请注明出处:https://www.506064.com/n/375246.html
微信扫一扫
支付宝扫一扫