提交 3ef1659f 编写于 作者: W wizardforcel

ch7.

上级 dbdfce64
......@@ -474,3 +474,110 @@ cones.group('Flavor')
| chocolate | 3 |
| strawberry | 2 |
有两个不同的类别,巧克力和草莓。 `group`的调用会在每个类别中创建一个计数表。 该列默认称为`count`,并包含每个类别中的行数。
注意,这一切都可以从`Flavor`列中找到。`Price `列尚未使用。
但是如果我们想要每种不同风味的圆筒的总价格呢? 这是`group`的第二个参数的作用。
### 发现每个类别的特征
`group`的可选的第二个参数是一个函数,用于聚合所有这些行的其他列中的值。 例如,`sum`将累计与每个类别匹配的所有行中的价格。 这个结果中,被分组的列中每个唯一值是一行,但与原始表列数相同。
为了找到每种口味的总价格,我们再次调用`group`,用`Flavor`作为第一个参数。 但这一次有第二个参数:函数名称`sum`
```py
cones.group('Flavor', sum)
```
| Flavor | Price sum |
| --- | --- |
| chocolate | 16.55 |
| strawberry | 8.8 |
为了创建这个新表格,`group`已经计算了对应于每种不同口味的,所有行中的`Price`条目的总和。 三个`chocolate`行的价格共计`$16.55`(您可以假设价格是以美元计量的)。 两个`strawberry`行的价格共计`8.80`
新创建的“总和”列的标签是`Price sum`,它通过使用被求和列的标签,并且附加单词`sum`创建。
由于`group`计算除了类别之外的所有列的`sum`,因此不需要指定必须对价格求和。
为了更详细地了解`group`在做什么,请注意,您可以自己计算总价格,不仅可以通过心算,还可以使用代码。 例如,要查找所有巧克力圆筒的总价格,您可以开始创建一个仅包含巧克力圆筒的新表,然后访问价格列:
```py
cones.where('Flavor', are.equal_to('chocolate')).column('Price')
array([ 4.75, 6.55, 5.25])
sum(cones.where('Flavor', are.equal_to('chocolate')).column('Price'))
16.550000000000001
```
这就是`group ``Flavor`中每个不同的值所做的事情。
```py
# For each distinct value in `Flavor, access all the rows
# and create an array of `Price`
cones_choc = cones.where('Flavor', are.equal_to('chocolate')).column('Price')
cones_strawb = cones.where('Flavor', are.equal_to('strawberry')).column('Price')
# Display the arrays in a table
grouped_cones = Table().with_columns(
'Flavor', make_array('chocolate', 'strawberry'),
'Array of All the Prices', make_array(cones_choc, cones_strawb)
)
# Append a column with the sum of the `Price` values in each array
price_totals = grouped_cones.with_column(
'Sum of the Array', make_array(sum(cones_choc), sum(cones_strawb))
)
price_totals
```
| Flavor | Array of All the Prices | Sum of the Array |
| --- | --- | --- |
| chocolate | [ 4.75 6.55 5.25] | 16.55 |
| strawberry | [ 3.55 5.25] | 8.8 |
你可以用任何其他可以用于数组的函数来替换`sum`。 例如,您可以使用`max`来查找每个类别中的最大价格:
```py
cones.group('Flavor', max)
```
| Flavor | Price max |
| --- | --- |
| chocolate | 6.55 |
| strawberry | 5.25 |
同样,`group`在每个`Flavor`分类中创建价格数组,但现在它寻找每个数组的`max `
```py
price_maxes = grouped_cones.with_column(
'Max of the Array', make_array(max(cones_choc), max(cones_strawb))
)
price_maxes
```
| Flavor | Array of All the Prices | Max of the Array |
| --- | --- | --- |
| chocolate | [ 4.75 6.55 5.25] | 6.55 |
| strawberry | [ 3.55 5.25] | 5.25 |
实际上,只有一个参数的原始调用,与使用`len`作为函数并清理表格的效果相同。
```py
lengths = grouped_cones.with_column(
'Length of the Array', make_array(len(cones_choc), len(cones_strawb))
)
lengths
```
| Flavor | Array of All the Prices | Length of the Array |
| --- | --- | --- |
| chocolate | [ 4.75 6.55 5.25] | 3 |
| strawberry | [ 3.55 5.25] | 2 |
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册