Pandas apply()函数允许用户传递一个函数并将其应用于Pandas Series的每个单个值。此函数提高了Pandas库的函数,因为它有助于根据所需条件隔离数据。这样它就可以有效地用于数据科学和机器学习。
要传递给函数的对象是Series对象,其索引是DataFrame的索引(即axis = 0)或DataFrame的列(即axis = 1)。默认情况下, result_type = None 和最终返回类型是从应用函数的返回类型推断出来的。否则,它取决于 result_type 参数。
DataFrame.apply(func, axis=0, broadcast=None, raw=False, reduce=None, result_type=None, args=(), **kwds)
它返回沿DataFrame的给定轴应用func的结果。
链接:https://www.learnfk.comhttps://www.learnfk.com/pandas/pandas-apply.html
来源:LearnFk无涯教程网
info = pd.DataFrame([[2, 7]] * 4, columns=['P', 'Q']) info.apply(np.sqrt) info.apply(np.sum, axis=0) info.apply(np.sum, axis=1) info.apply(lambda x: [1, 2], axis=1) info.apply(lambda x: [1, 2], axis=1, result_type='expand') info.apply(lambda x: pd.Series([1, 2], index=['foo', 'bar']), axis=1) info.apply(lambda x: [1, 2], axis=1, result_type='broadcast') info
输出
A B 0 2 7 1 2 7 2 2 7 3 2 7
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)