在任何用例中迭代数据帧时,iterrows 是否比 itertuples 更快?

问题描述 投票:0回答:1

我在许多文档中读到,在迭代数据帧时,itertuples 比 iterrows 更快。但在实现时,itertuples 相对较慢。我为此编写了一个测试代码。有人可以解释一下吗

statusMarked = result6[result6.mapping_id_id == row.id]
time1_tup=time.time();
for index,row in statusMarked.iterrows():   
    ap_1=0
time2_tup=time.time()
for row in statusMarked.itertuples():    
    ap_2=0
time3_tup=time.time()
print "row time "
print time2_tup-time1_tup
print "tuple time "
print time3_tup-time2_tup
#iterrows took .00099 seconds but itertuples took .002 seconds
pandas dataframe
1个回答
0
投票

您可能想检查以下相关的堆栈交换(已回答)问题:为什么 Pandas itertuples 在具有许多(> 100)列的数据帧上比 iterrows 慢?

© www.soinside.com 2019 - 2024. All rights reserved.