In [21]: df.groupby('key1').mean() Out[21]: data1 data2 key1 a 0.746672 0.910916 b -0.537585 0.525384 In [22]: df.groupby(['key1', 'key2']).mean() Out[22]: data1 data2 key1 key2 a one 0.880536 1.319920 two 0.478943 0.092908 b one -0.519439 0.281746 two -0.555730 0.769023...
print(data.groupby(['group1', 'group2']).min()) # Get min by multiple groups # x1 x2 # group1 group2 # A a 8 14 # b 6 16 # B a 2 12 # b 5 17 # C a 6 11 # b 1 15Video & Further ResourcesWould you like to learn more about the computation of maxima and minima ...
索引名为groupby 的分组键 ''' key1 a 0.746672 b -0.537585 Name: data1, dtype: float64 ''' # 数据(一个Series)根据分组键进行了聚合,并产生新的Series,其索引名称为
To achieve this, we have to specify a list of group columns within the groupby function.Consider the Python syntax below:print(data.groupby(['group1', 'group2']).mean()) # Get mean by multiple groups # x1 x2 # group1 group2 # A a 4.5 12.0 # b 8.0 18.0 # B a 5.0 12.0 # ...
无论你准备拿groupby做什么,都有可能会用到GroupBy的size方法,它可以返回一个含有分组大小的Series: In[23]:df.groupby(['key1','key2']).size()Out[23]:key1key2aone2two1bone1two1dtype:int64 注意,任何分组关键词中的缺失值,都会被从结果中除去。
s_grouped=df.groupby(['key1','key2'])['data2']print(s_grouped.mean()) 10.2.数据聚合 使用自己创建的聚合函数: defpeak_to_peak(arr):returnarr.max()-arr.min()print(grouped.agg(peak_to_peak)) 10.3. 应用 按smoker 分组后,选出小费最高的五组: ...
hier_df.groupby(level='city',axis=1).count() 6、聚合 #自定义聚合函数defpeak_to_peak(arr):returnarr.max() -arr.min() grouped.agg(peak_to_peak)#将定义的函数传入agg方法即可#面向列的多函数应用tips = pd.read_csv('examples/tips.csv') ...
GroupBy对象支持迭代,可以产生一组二元元组(由分组名和数据块组成)。看下面的例子: 代码语言:javascript 代码运行次数:0 复制 Cloud Studio代码运行 In[24]:forname,groupindf.groupby('key1'):...:print(name)...:print(group)...:a data1 data2 key1 key20-0.2047081.393406a one10.4789430.092908a two...
df.groupby('Category')- groups thedfDataFrame by the unique values in theCategorycolumn. ['Sales']- specifies that we are interested in theSalescolumn within each group. .sum()- calculates the sum of theSalesvalues for each group.
主要章节和小节重新按照如下逻辑划分: 一、Python基础 1 数字 2 字符串 3 列表 4 流程控制 5 编程风格 6 函数 7 输入和输出 8 数据结构 9 模块 10 错误和异常 11 类和对象 二、Python模块 1 时间模块 2 文件操作 3 常见迭代器 4 yield 用法 5 装饰