熊猫分位数功能很慢

时间:2015-11-16 20:57:13

标签: python numpy pandas

我想计算Pandas Dataframe上的分位数/百分位数。但是,功能非常慢。我用Numpy重复了一遍,我发现在Pandas中计算它会花费近10 000倍!

有人知道为什么会这样吗?我应该使用Numpy计算它,然后创建一个新的DataFrame而不是使用Pandas吗?

请参阅下面的代码:

import time
import pandas as pd
import numpy as np

q = np.array([0.1,0.4,0.6,0.9])
data = np.random.randn(10000, 4)
df = pd.DataFrame(data, columns=['a', 'b', 'c', 'd'])
time1 = time.time()
pandas_quantiles = df.quantile(q, axis=1)
time2 = time.time()
print 'Pandas took %0.3f ms' % ((time2-time1)*1000.0)

time1 = time.time()
numpy_quantiles = np.percentile(data, q*100, axis=1)
time2 = time.time()
print 'Numpy took %0.3f ms' % ((time2-time1)*1000.0)

print (pandas_quantiles.values == numpy_quantiles).all()
# Output:
# Pandas took 15337.531 ms
# Numpy took 1.653 ms
# True

1 个答案:

答案 0 :(得分:0)

使用python 3解决了最新版本的Pandas的问题。 在小型阵列上,熊猫的长度不到两倍,而在大型阵列上,熊猫的差异是5%。

我用pandas 0.24.1和Python 3得到以下输出:

import time
import pandas as pd
import numpy as np

q = np.array([0.1,0.4,0.6,0.9])
data = np.random.randn(10000, 4)
df = pd.DataFrame(data, columns=['a', 'b', 'c', 'd'])
time1 = time.time()
pandas_quantiles = df.quantile(q, axis=1)
time2 = time.time()
print 'Pandas took %0.3f ms' % ((time2-time1)*1000.0)

time1 = time.time()
numpy_quantiles = np.percentile(data, q*100, axis=1)
time2 = time.time()
print 'Numpy took %0.3f ms' % ((time2-time1)*1000.0)

print (pandas_quantiles.values == numpy_quantiles).all()
# Output:
# Pandas took 3.415 ms
# Numpy took 2.040 ms
# True