且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么 pandas 申请两次计算

更新时间:2023-02-05 20:09:52

可能与此问题.使用groupby时,应用函数将被额外调用一次,以查看是否可以进行某些优化.我猜这里正在发生类似的事情.目前看来还没有解决的办法(尽管我可能对您所看到的行为的来源有误).您是否有理由不进行额外的呼叫?

Probably related to this issue. With groupby, the applied function is called one extra time to see if certain optimizations can be done. I'd guess something similar is going on here. It doesn't look like there's any way around it at the moment (although I could be wrong about the source of the behavior you're seeing). Is there a reason you need it to not do that extra call.

此外,在您应用到该列时,调用它四次也是正常的.当您获得一列时,您将获得一个Series,而不是DataFrame.系列上的apply将功能应用于每个 元素.由于您的列中包含四个元素,因此该函数被调用了四次.

Also, calling it four times when you apply on the column is normal. When you get one columnm you get a Series, not a DataFrame. apply on a Series applies the function to each element. Since your column has four elements in it, the function is called four times.