用法:
GroupBy.apply(function)
在分組的塊上應用 python 轉換函數。
- func:函數
將應用於分組塊的 python 轉換函數。
參數:
例子:
from cudf import DataFrame df = DataFrame() df['key'] = [0, 0, 1, 1, 2, 2, 2] df['val'] = [0, 1, 2, 3, 4, 5, 6] groups = df.groupby(['key']) # Define a function to apply to each row in a group def mult(df): df['out'] = df['key'] * df['val'] return df result = groups.apply(mult) print(result)
輸出:
key val out 0 0 0 0 1 0 1 0 2 1 2 2 3 1 3 3 4 2 4 8 5 2 5 10 6 2 6 12
Pandas 兼容性說明
groupby.apply
與 pandas 相比,cuDF 的
groupby.apply
是有限的。在某些情況下,Pandas 將分組鍵作為索引的一部分返回,而 cudf 由於冗餘而不返回。例如:>>> df = pd.DataFrame({ 'a': [1, 1, 2, 2], 'b': [1, 2, 1, 2], 'c': [1, 2, 3, 4]}) >>> gdf = cudf.from_pandas(df) >>> df.groupby('a').apply(lambda x: x.iloc[[0]]) a b c a 1 0 1 1 1 2 2 2 1 3 >>> gdf.groupby('a').apply(lambda x: x.iloc[[0]]) a b c 0 1 1 1 2 2 1 3
相關用法
- Python cudf.core.groupby.groupby.GroupBy.agg用法及代碼示例
- Python cudf.core.groupby.groupby.GroupBy.corr用法及代碼示例
- Python cudf.core.groupby.groupby.GroupBy.pipe用法及代碼示例
- Python cudf.core.groupby.groupby.DataFrameGroupBy.describe用法及代碼示例
- Python cudf.core.groupby.groupby.DataFrameGroupBy.fillna用法及代碼示例
- Python cudf.core.groupby.groupby.SeriesGroupBy.aggregate用法及代碼示例
- Python cudf.core.groupby.groupby.DataFrameGroupBy.aggregate用法及代碼示例
- Python cudf.core.column.string.StringMethods.is_vowel用法及代碼示例
- Python cudf.core.column.string.StringMethods.endswith用法及代碼示例
- Python cudf.core.column.string.StringMethods.title用法及代碼示例
- Python cudf.core.column.string.StringMethods.contains用法及代碼示例
- Python cudf.core.column.string.StringMethods.rsplit用法及代碼示例
- Python cudf.core.column.string.StringMethods.zfill用法及代碼示例
- Python cudf.core.series.DatetimeProperties.month用法及代碼示例
- Python cudf.core.column.string.StringMethods.hex_to_int用法及代碼示例
- Python cudf.core.column.string.StringMethods.htoi用法及代碼示例
- Python cudf.core.series.DatetimeProperties.year用法及代碼示例
- Python cudf.core.column.string.StringMethods.character_tokenize用法及代碼示例
- Python cudf.core.column.string.StringMethods.normalize_characters用法及代碼示例
- Python cudf.core.column.string.StringMethods.filter_alphanum用法及代碼示例
注:本文由純淨天空篩選整理自rapids.ai大神的英文原創作品 cudf.core.groupby.groupby.GroupBy.apply。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。