Python pandas.DataFrame.apply用法

今天被pandas.DataFrame.apply和pandas.core.groupby.GroupBy.apply搞糊涂了,在搜索的过程中发现很多人跟我一样弄糊涂了。

这是

DataFrame.apply

 

 Applies function along input axis of DataFrame.

        Objects passed to functions are Series objects having index
        either the DataFrame's index (axis=0) or the columns (axis=1).
        Return type depends on whether passed function aggregates, or the
        reduce argument if the DataFrame is empty.

 

这是pandas.core.groupby.GroupBy.apply

 GroupBy.apply(func, *args, **kwargs)[source]

    Apply function func group-wise and combine the results together.

    The function passed to apply must take a dataframe as its first argument and return a dataframe, a series or a scalar. apply will then take care of combining the results back together into a single dataframe or series. apply is therefore a highly flexible grouping method.

    While apply is a very flexible method, its downside is that using it can be quite a bit slower than using more specific methods. Pandas offers a wide range of method that will be much faster than using apply for their specific purposes, so try to use them before reaching for apply.

很明显这两完全不是一回事,一个是将Series传给函数,一个是dataframe。

还有人说DataFrame.apply  “既可以作用于一行或者一列的元素,也可以作用于单个元素。”,从结果上来看这个说法有时是对的,但作用于单个元素的实质是Series具有numpy广播的特性,并不是真的像applymap一样作用在每个元素上。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值