如何使用 OML4Py oml.group_apply 调用按两列进行分组?
例如在 sql 中我可以执行以下操作:
''' 从 emp 中选择 mgr、count(mgr)、count(deptno)、deptno 按经理、部门分组 按部门订购;
返回 7782 1 1 10 7839 1 1 10 0 1 10 7566 2 2 20 7788 1 1 20 7839 1 1 20 7902 1 1 20 7698 5 5 30 7839 1 1 30 '''
考虑以下 group_count 函数:
%python
def group_count(dat):
import pandas as pd
return pd.DataFrame([(dat["col1"][0], dat.shape[0])],\
columns = ["col1", "COUNT"])
要按两个分组类别进行分组,请在索引参数中指定两列:
index = DF[:,['col2', 'col3']]
其中 DF 是您的 oml 代理对象,col2 和 col3 是您的分组变量。
res = oml.group_apply(oml_iris, index,
func=group_count,
oml_input_type="pandas.DataFrame")