繁体   English   中英

如何通过在Graphlab SFrame中划分两列来创建新列?

[英]How to create a new column by dividing two columns in Graphlab SFrame?

给定这样的Graphlab SFrame:

+-------+------------+---------+-----------+
| Store |    Date    |  Sales  | Customers |
+-------+------------+---------+-----------+
|   1   | 2015-07-31 |  5263.0 |   555.0   |
|   2   | 2015-07-31 |  6064.0 |   625.0   |
|   3   | 2015-07-31 |  8314.0 |   821.0   |
|   4   | 2015-07-31 | 13995.0 |   1498.0  |
|   3   | 2015-07-20 |  4822.0 |   559.0   |
|   2   | 2015-07-10 |  5651.0 |   589.0   |
|   4   | 2015-07-11 | 15344.0 |   1414.0  |
|   5   | 2015-07-23 |  8492.0 |   833.0   |
|   2   | 2015-07-19 |  8565.0 |   687.0   |
|   10  | 2015-07-09 |  7185.0 |   681.0   |
+-------+------------+---------+-----------+
[986159 rows x 4 columns]

如何通过将销售除以客户的每一行来添加“每位客户的销售额”列?

我尝试了以下方法,但它们不起作用( sf是我的SFrame

sf['salespercustomer'] = sf.apply(lambda x: sf['Sales']/sf['Customers'])

有趣的是,我得到一个SArray的输出:

sf['Sales'] / sf['Customers']

但这实际上并没有帮助将列添加回sf ,所以这不起作用=(:

sf['salescustomer'] = sf['Sales'] / sf['Customers']

最后一行代码应该可以解决问题,但是您说您的SFrame称为sf ,而不是train 当我尝试使用sf ,效果很好。

这就是我要做的。

sf['salespercustomer'] = sf['Sales','Customers'].apply(lambda row: row['Sales']/row['Customers'])

FWIW,您的样本将整个sf传递给apply lambda作为参数x,但是您使用了sf。 我的理解是在lambda函数中不知道sf,但是x的别名是。

FWIW,您可以像这样执行单列操作:

sf['sales25percentdiscount'] = sf['Sales'].apply(lambda x: x*.0.75)

由于仅指定了一列,因此您无需在lambda函数中指定列。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM