mars.dataframe.groupby.GroupBy.head#

GroupBy.head(n=5)#

返回每个组的前 n 行。

类似于 .apply(lambda x: x.head(n)),但它返回原始 Series 或 DataFrame 的一部分行,保留原始索引和顺序(as_index 标志被忽略)。

对负值的n无效。

Return type

序列数据框

另请参阅

Series.groupby, DataFrame.groupby

示例

>>> import mars.dataframe as md
>>> df = md.DataFrame([[1, 2], [1, 4], [5, 6]],
...                   columns=['A', 'B'])
>>> df.groupby('A').head(1).execute()
   A  B
0  1  2
2  5  6
>>> df.groupby('A').head(-1).execute()
Empty DataFrame
Columns: [A, B]
Index: []