GraphLab SFrame:对列中的所有值求和

5
如何对SFrame graphlab中的一列进行求和。我尝试查看官方文档,但只提供了SaArray(doc)的说明,没有任何示例。
2个回答

4
>>> import graphlab as gl
>>> sf = gl.SFrame({'foo':[1,2,3], 'bar':[4,5,6]})
>>> sf
Columns:
        bar     int
        foo     int

Rows: 3

Data:
+-----+-----+
| bar | foo |
+-----+-----+
|  4  |  1  |
|  5  |  2  |
|  6  |  3  |
+-----+-----+
[3 rows x 2 columns]
>>> sf['foo'].sum()
6

3

我认为提问者的问题更多地涉及如何一次性跨越所有列(或列出的列)进行此操作。以下是pandas和graphlab之间的比较。

# imports
import graphlab as gl    
import pandas as pd
import numpy as np

# generate data
data = np.random.randint(0,10,size=100).reshape(10,10)
col_names = list('ABCDEFGHIJ')

# make dataframe and sframe
df = pd.DataFrame(data, columns=names)
sf = graphlab.SFrame(df)

# get sum for all columns (pandas).  Returns a series.
df.sum().sort_values(ascending=False)

D    65
A    61
J    59
B    50
H    46
G    46
I    45
F    43
C    37
E    36

# sf.sum() does not work
# get sum for each of the columns (graphlab)
for col in col_names:
    print col, sf[col].sum()

A 61
B 50
C 37
D 65
E 36
F 43
G 46
H 46
I 45
J 59

我有同样的问题。Pandas提供了一个简单的接口,可以在数据框的行或列上应用聚合函数。那么SFrame有没有相同的功能呢?我只能想到迭代列列表来实现。是否有更好的方法?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接