2017-05-31 84 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])