2017-05-31 140 views
0

我想檢查一串熊貓數據幀的索引是否相同。我寫了下面的函數,它接受一個數據框的元組作爲輸入。檢查多個熊貓數據幀索引的等同性

def chk_index_match(*dfs): 
    ran_once = False 
    for df in dfs: 
     if not ran_once: 
      ref_df = df 
      ran_once = True 
     else: 
      if not(ref_df.index.equals(df.index)): 
       return False 
    return True 

是否有內建熊貓功能可以做類似的事情?或者更好的方式呢?

回答

0

這裏是另一種方式,我發現,不知道這是否是因爲這樣更好,但它使用了更多的現成的功能和更少的代碼:

import numpy as np 
def chk_index_match(*dfs): 
    arrays = [np.array(df.index) for df in dfs] 
    return np.all([np.array_equal(arrays[0], a) for a in arrays])