FunctionDescription
mean() Compute mean of groups
sum() Compute sum of group values
size() Compute group sizes
count() Compute count of group
std() Standard deviation of groups
var() Compute variance of groups
sem() Standard error of the mean of groups
describe() Generates descriptive statistics
first() Compute first of group values
last() Compute last of group values
nth() Take nth value, or a subset if n is a list
min() Compute min of group values
max() Compute max of group values
np.random.seed(123)
df = pd.DataFrame({'A' : ['foo', 'foo', 'bar', 'foo', 'bar', 'foo'],
'B' : ['one', 'two', 'three','two', 'two', 'one'],
'C' : np.random.randint(5, size=6),
'D' : np.random.randint(5, size=6),
'E' : np.random.randint(5, size=6)})
print (df)
A B C D E
0 foo one 2 3 0
1 foo two 4 1 0
2 bar three 2 1 1
3 foo two 1 0 3
4 bar two 3 1 4
5 foo one 2 1 0
df1 = df.groupby(['A', 'B'], as_index=False)['C'].agg('sum')
print (df1)
A B C
0 bar three 2
1 bar two 3
2 foo one 4
3 foo two 5
df2 = df.groupby(['A', 'B'], as_index=False).agg('sum')
print (df2)
A B C D E
0 bar three 2 1 1
1 bar two 3 1 4
2 foo one 4 4 0
3 foo two 5 1 3
对于应用于一列的乘法函数,使用tuples的列表-新列和aggregted函数的名称:
df4 = (df.groupby(['A', 'B'])['C']
.agg([('average','mean'),('total','sum')])
.reset_index())
print (df4)
A B average total
0 bar three 2.0 2
1 bar two 3.0 3
2 foo one 2.0 4
3 foo two 2.5 5
如果要传递多个函数,则可以传递tuples的list:
df5 = (df.groupby(['A', 'B'])
.agg([('average','mean'),('total','sum')]))
print (df5)
C D E
average total average total average total
A B
bar three 2.0 2 1.0 1 1.0 1
two 3.0 3 1.0 1 4.0 4
foo one 2.0 4 2.0 4 0.0 0
two 2.5 5 0.5 1 1.5 3
df5 = df.groupby(['A', 'B']).agg(['mean','sum'])
df5.columns = (df5.columns.map('_'.join)
.str.replace('sum','total')
.str.replace('mean','average'))
df5 = df5.reset_index()
print (df5)
A B C_average C_total D_average D_total E_average E_total
0 bar three 2.0 2 1.0 1 1.0 1
1 bar two 3.0 3 1.0 1 4.0 4
2 foo one 2.0 4 2.0 4 0.0 0
3 foo two 2.5 5 0.5 1 1.5 3
如果要用聚合函数分别指定每个列,请传递dictionary:
df6 = (df.groupby(['A', 'B'], as_index=False)
.agg({'C':'sum','D':'mean'})
.rename(columns={'C':'C_total', 'D':'D_average'}))
print (df6)
A B C_total D_average
0 bar three 2 1.0
1 bar two 3 1.0
2 foo one 4 2.0
3 foo two 5 0.5
您也可以传递自定义函数:
def func(x):
return x.iat[0] + x.iat[-1]
df7 = (df.groupby(['A', 'B'], as_index=False)
.agg({'C':'sum','D': func})
.rename(columns={'C':'C_total', 'D':'D_sum_first_and_last'}))
print (df7)
A B C_total D_sum_first_and_last
0 bar three 2 2
1 bar two 3 2
2 foo one 4 4
3 foo two 5 1
问题2
聚合后没有数据帧!怎么搞的?
按2列或更多列聚合:
df1 = df.groupby(['A', 'B'])['C'].sum()
print (df1)
A B
bar three 2
two 3
foo one 4
two 5
Name: C, dtype: int32
df2 = df.groupby('A', as_index=False)['C'].sum()
print (df2)
A C
0 bar 5
1 foo 9
df2 = df.groupby('A')['C'].sum().reset_index()
print (df2)
A C
0 bar 5
1 foo 9
问题3
如何主要聚合字符串列(到lists,tuples,strings with separator)?
df = pd.DataFrame({'A' : ['a', 'c', 'b', 'b', 'a', 'c', 'b'],
'B' : ['one', 'two', 'three','two', 'two', 'one', 'three'],
'C' : ['three', 'one', 'two', 'two', 'three','two', 'one'],
'D' : [1,2,3,2,3,1,2]})
print (df)
A B C D
0 a one three 1
1 c two one 2
2 b three two 3
3 b two two 2
4 a two three 3
5 c one two 1
6 b three one 2
代替aggregeta函数的可能是转换列的passlist、tuple、set:
df1 = df.groupby('A')['B'].agg(list).reset_index()
print (df1)
A B
0 a [one, two]
1 b [three, two, three]
2 c [two, one]
df4 = df.groupby('A').agg(','.join).reset_index()
print (df4)
A B C
0 a one,two three,three
1 b three,two,three two,two,one
2 c two,one one,two
因此有必要将所有列转换为字符串,然后获取所有列:
df5 = (df.groupby('A')
.agg(lambda x: ','.join(x.astype(str)))
.reset_index())
print (df5)
A B C D
0 a one,two three,three 1,3
1 b three,two,three two,two,one 3,2,2
2 c two,one one,two 2,1
问题4
如何合计计数?
df = pd.DataFrame({'A' : ['a', 'c', 'b', 'b', 'a', 'c', 'b'],
'B' : ['one', 'two', 'three','two', 'two', 'one', 'three'],
'C' : ['three', np.nan, np.nan, 'two', 'three','two', 'one'],
'D' : [np.nan,2,3,2,3,np.nan,2]})
print (df)
A B C D
0 a one three NaN
1 c two NaN 2.0
2 b three NaN 3.0
3 b two two 2.0
4 a two three 3.0
5 c one two NaN
6 b three one 2.0
问题1
如何与熊猫进行聚集?
展开的aggregation documentation。
聚合函数是减少返回对象维度的函数。这意味着输出序列/数据帧的行数少于或与原始行数相同。 下面列出了一些常见的聚合函数:
按筛选列和cython implemented functions聚合:
aaaggregate函数用于未在
groupby
函数中指定的所有列,这里是A, B
列:您还可以只指定一些列,用于在
groupby
函数后的列表中进行聚合:使用函数^{} 得到相同的结果:
对于应用于一列的乘法函数,使用
tuple
s的列表-新列和aggregted函数的名称:如果要传递多个函数,则可以传递
tuple
s的list
:然后在列中获取
MultiIndex
:对于转换为列,将
MultiIndex
与join
一起使用map
:另一种解决方案是传递聚合函数列表,然后展平} :
MultiIndex
,对于其他列名,使用^{如果要用聚合函数分别指定每个列,请传递
dictionary
:您也可以传递自定义函数:
问题2
聚合后没有数据帧!怎么搞的?
按2列或更多列聚合:
首先检查pandas对象的
Index
和type
:有两种解决方案可用于将
MultiIndex Series
获取到列:as_index=False
如果按一列分组:
。。。使用
Index
获取Series
:解决方案与
MultiIndex Series
中的相同:问题3
如何主要聚合字符串列(到
list
s,tuple
s,strings with separator
)?代替aggregeta函数的可能是转换列的pass
list
、tuple
、set
:替代方法是使用^{} :
若要转换为带分隔符的字符串,请仅在以下情况下使用
.join
:如果数值列与^{} 一起使用lambda函数转换为
string
s:另一种解决方案是在
groupby
之前转换为字符串:对于转换所有列,在
groupby
之后不传递列列表。 没有列D
,因为automatic exclusion of 'nuisance' columns, 这意味着排除了所有数值列。因此有必要将所有列转换为字符串,然后获取所有列:
问题4
如何合计计数?
每个组的} :
size
的函数^{函数^{} 排除缺少的值:
对于count non missing值,应在多个列中使用函数:
相关函数^{}
返回size对象,该对象包含按降序排列的唯一值的计数,因此第一个元素是最常出现的元素。默认情况下排除
NaN
s值。如果您想要像使用函数} :
groupby
+size
一样的输出,请添加^{问题5
如何创建由聚合值填充的新列?
方法^{} 返回一个索引与被分组对象相同(大小相同)的对象
熊猫documentation了解更多信息。
相关问题 更多 >
编程相关推荐