在逐 block 构建大型数据帧时,我对 Pandas 的性能感到困惑。在 Numpy 中,我们(几乎)总是通过预分配一个大的空数组然后填充值来获得更好的性能。据我了解,这是由于 Numpy 一次抓取了它需要的所有内存,而不是每次 append 操作都必须重新分配内存。
在 Pandas 中,我似乎通过使用 df = df.append(temp) 模式获得了更好的性能。
这是一个计时示例。 Timer 类的定义如下。正如你所见,我发现预分配比使用 append 慢大约 10 倍!使用适当 dtype 的 np.empty 值预分配数据帧有很大帮助,但 append 方法仍然是最快的。
import numpy as np
from numpy.random import rand
import pandas as pd
from timer import Timer
# Some constants
num_dfs = 10 # Number of random dataframes to generate
n_rows = 2500
n_cols = 40
n_reps = 100 # Number of repetitions for timing
# Generate a list of num_dfs dataframes of random values
df_list = [pd.DataFrame(rand(n_rows*n_cols).reshape((n_rows, n_cols)), columns=np.arange(n_cols)) for i in np.arange(num_dfs)]
##
# Define two methods of growing a large dataframe
##
# Method 1 - append dataframes
def method1():
out_df1 = pd.DataFrame(columns=np.arange(4))
for df in df_list:
out_df1 = out_df1.append(df, ignore_index=True)
return out_df1
def method2():
# # Create an empty dataframe that is big enough to hold all the dataframes in df_list
out_df2 = pd.DataFrame(columns=np.arange(n_cols), index=np.arange(num_dfs*n_rows))
#EDIT_1: Set the dtypes of each column
for ix, col in enumerate(out_df2.columns):
out_df2[col] = out_df2[col].astype(df_list[0].dtypes[ix])
# Fill in the values
for ix, df in enumerate(df_list):
out_df2.iloc[ix*n_rows:(ix+1)*n_rows, :] = df.values
return out_df2
# EDIT_2:
# Method 3 - preallocate dataframe with np.empty data of appropriate type
def method3():
# Create fake data array
data = np.transpose(np.array([np.empty(n_rows*num_dfs, dtype=dt) for dt in df_list[0].dtypes]))
# Create placeholder dataframe
out_df3 = pd.DataFrame(data)
# Fill in the real values
for ix, df in enumerate(df_list):
out_df3.iloc[ix*n_rows:(ix+1)*n_rows, :] = df.values
return out_df3
##
# Time both methods
##
# Time Method 1
times_1 = np.empty(n_reps)
for i in np.arange(n_reps):
with Timer() as t:
df1 = method1()
times_1[i] = t.secs
print 'Total time for %d repetitions of Method 1: %f [sec]' % (n_reps, np.sum(times_1))
print 'Best time: %f' % (np.min(times_1))
print 'Mean time: %f' % (np.mean(times_1))
#>> Total time for 100 repetitions of Method 1: 2.928296 [sec]
#>> Best time: 0.028532
#>> Mean time: 0.029283
# Time Method 2
times_2 = np.empty(n_reps)
for i in np.arange(n_reps):
with Timer() as t:
df2 = method2()
times_2[i] = t.secs
print 'Total time for %d repetitions of Method 2: %f [sec]' % (n_reps, np.sum(times_2))
print 'Best time: %f' % (np.min(times_2))
print 'Mean time: %f' % (np.mean(times_2))
#>> Total time for 100 repetitions of Method 2: 32.143247 [sec]
#>> Best time: 0.315075
#>> Mean time: 0.321432
# Time Method 3
times_3 = np.empty(n_reps)
for i in np.arange(n_reps):
with Timer() as t:
df3 = method3()
times_3[i] = t.secs
print 'Total time for %d repetitions of Method 3: %f [sec]' % (n_reps, np.sum(times_3))
print 'Best time: %f' % (np.min(times_3))
print 'Mean time: %f' % (np.mean(times_3))
#>> Total time for 100 repetitions of Method 3: 6.577038 [sec]
#>> Best time: 0.063437
#>> Mean time: 0.065770
我使用了一个不错的 Timer,由 Huy Nguyen 提供:
# credit: http://www.huyng.com/posts/python-performance-analysis/
import time
class Timer(object):
def __init__(self, verbose=False):
self.verbose = verbose
def __enter__(self):
self.start = time.clock()
return self
def __exit__(self, *args):
self.end = time.clock()
self.secs = self.end - self.start
self.msecs = self.secs * 1000 # millisecs
if self.verbose:
print 'elapsed time: %f ms' % self.msecs
如果你还在关注,我有两个问题:
1) 为什么 append 方法更快? (注意:对于非常小的数据帧,即 n_rows = 40,它实际上更慢)。
2) 用 block 构建大型数据框的最有效方法是什么? (在我的例子中,这些 block 都是大的 csv 文件)。
感谢您的帮助!
EDIT_1:
在我的真实世界项目中,这些列具有不同的 dtype。因此,根据 BrenBarn 的建议,我不能使用 pd.DataFrame(.... dtype=some_type) 技巧来提高预分配的性能。 dtype 参数强制所有列为相同的 dtype [Ref.问题 4464]
我在代码中的 method2() 中添加了一些行,以逐列更改 dtypes 以匹配输入数据帧。此操作代价高昂,并且在写入行 block 时抵消了具有适当 dtype 的好处。
EDIT_2:尝试使用占位符数组 np.empty(... dtyp=some_type) 预分配数据帧。根据@Joris 的建议。
最佳答案
您的基准实际上太小而无法显示真正的差异。 附加,每次复制,所以你实际上是在复制一个大小为 N 的内存空间 N*(N-1) 次。随着数据框大小的增长,这非常低效。在非常小的框架中,这当然可能无关紧要。但是,如果您有任何实际尺寸,这很重要。这在文档 here 中有特别说明。 ,虽然有点小警告。
In [97]: df = DataFrame(np.random.randn(100000,20))
In [98]: df['B'] = 'foo'
In [99]: df['C'] = pd.Timestamp('20130101')
In [103]: df.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 100000 entries, 0 to 99999
Data columns (total 22 columns):
0 100000 non-null float64
1 100000 non-null float64
2 100000 non-null float64
3 100000 non-null float64
4 100000 non-null float64
5 100000 non-null float64
6 100000 non-null float64
7 100000 non-null float64
8 100000 non-null float64
9 100000 non-null float64
10 100000 non-null float64
11 100000 non-null float64
12 100000 non-null float64
13 100000 non-null float64
14 100000 non-null float64
15 100000 non-null float64
16 100000 non-null float64
17 100000 non-null float64
18 100000 non-null float64
19 100000 non-null float64
B 100000 non-null object
C 100000 non-null datetime64[ns]
dtypes: datetime64[ns](1), float64(20), object(1)
memory usage: 17.5+ MB
追加
In [85]: def f1():
....: result = df
....: for i in range(9):
....: result = result.append(df)
....: return result
....:
连接
In [86]: def f2():
....: result = []
....: for i in range(10):
....: result.append(df)
....: return pd.concat(result)
....:
In [100]: f1().equals(f2())
Out[100]: True
In [101]: %timeit f1()
1 loops, best of 3: 1.66 s per loop
In [102]: %timeit f2()
1 loops, best of 3: 220 ms per loop
请注意,我什至不会费心尝试预先分配。它有点复杂,特别是因为您正在处理多种 dtypes(例如,您 可以 制作一个巨大的框架并简单地 .loc 并且它会起作用)。但是 pd.concat 非常简单,工作可靠且快速。
以及从上方确定尺寸的时间
In [104]: df = DataFrame(np.random.randn(2500,40))
In [105]: %timeit f1()
10 loops, best of 3: 33.1 ms per loop
In [106]: %timeit f2()
100 loops, best of 3: 4.23 ms per loop
关于python - 创建大型 Pandas DataFrames : preallocation vs append vs concat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31690076/
出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
如何使用RSpec::Core::RakeTask初始化RSpecRake任务?require'rspec/core/rake_task'RSpec::Core::RakeTask.newdo|t|#whatdoIputinhere?endInitialize函数记录在http://rubydoc.info/github/rspec/rspec-core/RSpec/Core/RakeTask#initialize-instance_method没有很好的记录;它只是说:-(RakeTask)initialize(*args,&task_block)AnewinstanceofRake
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?
我正在阅读SandiMetz的POODR,并且遇到了一个我不太了解的编码原则。这是代码:classBicycleattr_reader:size,:chain,:tire_sizedefinitialize(args={})@size=args[:size]||1@chain=args[:chain]||2@tire_size=args[:tire_size]||3post_initialize(args)endendclassMountainBike此代码将为其各自的属性输出1,2,3,4,5。我不明白的是查找方法。当一辆山地自行车被实例化时,因为它没有自己的initialize方法
我正在尝试按0-9和a-z的顺序创建数字和字母列表。我有一组值value_array=['0','1','2','3','4','5','6','7','8','9','a','b','光盘','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','','u','v','w','x','y','z']和一个组合列表的数组,按顺序,这些数字可以产生x个字符,比方说三个list_array=[]和一个当前字母和数字组合的数组(在将它插入列表数组之前我会把它变成一个字符串,]current_combo['0','0','0']
这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:Pythonconditionalassignmentoperator对于这样一个简单的问题表示歉意,但是谷歌搜索||=并不是很有帮助;)Python中是否有与Ruby和Perl中的||=语句等效的语句?例如:foo="hey"foo||="what"#assignfooifit'sundefined#fooisstill"hey"bar||="yeah"#baris"yeah"另外,类似这样的东西的通用术语是什么?条件分配是我的第一个猜测,但Wikipediapage跟我想的不太一样。
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht