Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
比较python中df.apply和Column操作的性能_Python_Performance_Pandas_Dataframe_Apply - Fatal编程技术网

比较python中df.apply和Column操作的性能

比较python中df.apply和Column操作的性能,python,performance,pandas,dataframe,apply,Python,Performance,Pandas,Dataframe,Apply,我想知道使用数据帧的列执行基本算术运算是以列方式还是通过apply执行更快。特别是,我认为columnwise更快。但这两种方式都被视为“矢量化”操作。那么,df.apply是否比较快?我们可以试试这个。下面的示例演示了按列操作的速度(要快得多): Thsis在我的机器上提供以下信息: Duration of apply: 0:00:23.631236 Duration of columnwise addition: 0:00:00.004234 Ratio: 0.000179169638

我想知道使用数据帧的列执行基本算术运算是以列方式还是通过apply执行更快。特别是,我认为columnwise更快。但这两种方式都被视为“矢量化”操作。那么,
df.apply
是否比较快?

我们可以试试这个。下面的示例演示了按列操作的速度(要快得多):

Thsis在我的机器上提供以下信息:

Duration of apply:  0:00:23.631236
Duration of columnwise addition:  0:00:00.004234
Ratio:  0.00017916963801639492
That means, columnwise addition is 5581.302786962683 times faster than addition via apply!

df.apply
未矢量化。它可能在语法上看起来很简单,但它只是一个薄薄的循环,比
df.iterrows
更快。
Duration of apply:  0:00:23.631236
Duration of columnwise addition:  0:00:00.004234
Ratio:  0.00017916963801639492
That means, columnwise addition is 5581.302786962683 times faster than addition via apply!