Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
data8-textbook-zh
提交
3ef1659f
D
data8-textbook-zh
项目概览
OpenDocCN
/
data8-textbook-zh
大约 1 年 前同步成功
通知
0
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
data8-textbook-zh
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
3ef1659f
编写于
11月 19, 2017
作者:
W
wizardforcel
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
ch7.
上级
dbdfce64
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
107 addition
and
0 deletion
+107
-0
7.md
7.md
+107
-0
未找到文件。
7.md
浏览文件 @
3ef1659f
...
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录