本文主要介绍了pandas中groupby操作实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
import numpy as np
import pandas as pd
df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'foo'],'B' : ['one', 'one', 'two', 'three', 'two', 'two', 'one', 'three'],'C' : np.random.randn(8),'D' : np.random.randn(8)})
print(df)
2.通过A列对df进行分布操作。
3.通过A、B列对df进行分组操作。
4…使用自定义函数进行分组操作,自定义一个函数,使用groupby方法并使用自定义函数给定的条件,按列对df进行分组。
def get_letter_type(letter):
if letter.lower() in 'aeiou':
return 'vowel'
else:
return 'consonant'
grouped = df.groupby(get_letter_type, axis=1)
for group in grouped:
print(group)
5.创建一个Series名为s,使用groupby根据s的索引对s进行分组,返回分组后的新Series,对新Series进行first、last、sum操作。
lst = [1, 2, 3, 1, 2, 3]
s = pd.Series([1, 2, 3, 10, 20, 30], lst)
grouped = s.groupby(level=0)
#查看分组后的第一行数据
grouped.first()
#查看分组后的最后一行数据
grouped.last()
#对分组的各组进行求和
grouped.sum()
6.分组排序,使用groupby进行分组时,默认是按分组后索引进行升序排列,在groupby方法中加入sort=False参数,可以进行降序排列。
df2=pd.DataFrame({'X':['B','B','A','A'],'Y':[1,2,3,4]})
#按X列对df2进行分组,并求每组的和
df2.groupby(['X']).sum()
#按X列对df2进行分组,分组时不对键进行排序,并求每组的和
df2.groupby(['X'],sort=False).sum()
7.使用get_group方法得到分组后某组的值。
df3 = pd.DataFrame({'X' : ['A', 'B', 'A', 'B'], 'Y' : [1, 4, 3, 2]})
#按X列df3进行分组,并得到A组的df3值
df3.groupby(['X']).get_group('A')
#按X列df3进行分组,并得到B组的df3值
df3.groupby(['X']).get_group('B')
8.使用groups方法得到分组后所有组的值。
df.groupby('A').groups
df.groupby(['A','B']).groups
9.多级索引分组,创建一个有两级索引的Series,并使用两个方法对Series进行分组并求和。
arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
index=pd.MultiIndex.from_arrays(arrays,names=['first','second'])
s=pd.Series(np.random.randn(8),index=index)
s.groupby(level=0).sum()
s.groupby(level='second').sum()
10.复合分组,对s按first、second进行分组并求和。
s.groupby(level=['first', 'second']).sum()
11.复合分组(按索引和列),创建数据帧df,使用索引级别和列对df进行分组。
arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
index = pd.MultiIndex.from_arrays(arrays, names=['first', 'second'])
df = pd.DataFrame({'A': [1, 1, 1, 1, 2, 2, 3, 3], 'B': np.arange(8)},index=index)
print(df)
df.groupby([pd.Grouper(level=1),'A']).sum()
12.对df进行分组,将分组后C列的值赋值给grouped,统计grouped中每类的个数。
df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'foo'],'B' : ['one', 'one', 'two', 'three', 'two', 'two', 'one', 'three'],'C' : np.random.randn(8),'D' : np.random.randn(8)})
grouped=df.groupby(['A'])
grouped_C=grouped['C']
print(grouped_C.count())
13.对上面创建的df的C列,按A列值进行分组并求和。
df['C'].groupby(df['A']).sum()
14.遍历分组结果,通过A,B两列对df进行分组,分组结果的组名为元组。
for name, group in df.groupby(['A', 'B']):
print(name)
print(group)
15.通过A列对df进行分组,并查看分组对象的bar列。
df.groupby(['A']).get_group(('bar'))
16.按A,B两列对df进行分组,并查看分组对象中bar、one都存在的部分。
df.groupby(['A','B']).get_group(('bar','one'))
注意:当分组按两列来分时,查看分组对象也应该包含每列的一部分。
17.聚合操作,按A列对df进行分组,使用聚合函数aggregate求每组的和。
grouped=df.groupby(['A']) grouped.aggregate(np.sum)
按A、B两列对df进行分组,并使用聚合函数aggregate对每组求和。
grouped=df.groupby(['A'])
grouped.aggregate(np.sum)
注意:通过上面的结果可以看到。聚合完成后每组都有一个组名作为新的索引,使用as_index=False可以忽略组名。
18.当as_index=True时,在groupby中使用的键将成为新的dataframe中的索引。按A、B两列对df进行分组,这是使参数as_index=False,再使用聚合函数aggregate求每组的和.
grouped=df.groupby(['A','B'],as_index=False)
grouped.aggregate(np.sum)
19.聚合操作,按A、B列对df进行分组,使用size方法,求每组的大小。返回一个Series,索引是组名,值是每组的大小。