就是分组
# 创建一个示例 DataFrame data = {'Category': ['A', 'B', 'A', 'B', 'A', 'B', 'A', 'A'], 'Value': [10, 20, 15, 25, 30, 35, 40, 45]} df = pd.DataFrame(data) print(df) Category Value 0 A 10 1 B 20 2 A 15 3 B 25 4 A 30 5 B 35 6 A 40 7 A 45
# 使用 groupby 分组 grouped = df.groupby('Category')
# 获取某个组
roup_A = grouped.get_group('A')
print(group_A)
Category Value
0 A 10
2 A 15
4 A 30
6 A 40
7 A 45
# 每个组的详细信息,将每个组转为list
for name, group in grouped:
print(f"Group: {name}")
print(group)
print(group["Value"].tolist())
Group: A
Category Value
0 A 10
2 A 15
4 A 30
6 A 40
7 A 45
[10, 15, 30, 40, 45]
Group: B
Category Value
1 B 20
3 B 25
5 B 35
[20, 25, 35]
标签:Category,group,45,35,print,groupby From: https://www.cnblogs.com/mxleader/p/17861533.html