2016-11-15 66 views
0
for i in range(1, len(df)): 
    if df.loc[i]["identification"] == df.loc[i-1]["identification"] and df.loc[i]["date"] == df.loc[i-1]["date"]: 
     df.loc[i,"duplicate"] = 1 
    else: 
     df.loc[i,"duplicate"] = 0 

當處理大尺寸的數據幀時,這種循環運行非常簡單。如何在使用Pandas數據框時避免():循環緩慢?

有什麼建議嗎?

+1

請提供更多細節:什麼是「慢」,什麼是「大尺寸」。 – Danra

回答

2

嘗試使用量化的方法,而不是循環的:

df['duplicate'] = np.where((df.identification == df.identification.shift()) 
          & 
          (df.date == df.date.shift()), 
          1,0) 
+0

太好了,這真的是我想要的,在運行時間上有了巨大的提高,謝謝。 –

0

看起來你只是檢查,如果值是重複的。在這種情況下,您可以使用

df.sort_values(by=['identification', 'date'], inplace=True) 
df['duplicate'] = df.duplicated(subset=['identification', 'date']).astype(int) 
+0

排序已經完成,但您的建議也可以正常工作,謝謝。 –