2017-01-26 45 views
0

我正在讀取一個表,在我們進一步處理之前,必須對其所有值進行驗證。有效值存儲在與我們的主表匹配的另一個表中。驗證標準是如下幾個匹配列:匹配表中的幾個列組合

Table 1 (the main data we read in) 

Name --- Unit --- Age --- Address --- Nationality 

以上表明,我們正在從表中讀取列名和其他表包含上述列的有效值。當我們僅查看主表中的有效值時,我們必須考慮主數據表中的列的組合,例如Name --- Unit --- Age。如果列組合的特定行中的所有值都與另一個表匹配,那麼我們保留該行,否則我們將其刪除。

如何解決與Numpy的問題?

謝謝

回答

0

你可以循環遍歷行。一個簡單/簡單的方法將是:

dummy_df = table_df ## make a copy of your table, since we are deleting rows we want to have the original df saved. 

relevant_columns = ['age','name','sex',...] ## define relevant columns, in case either dataframe has columns you dont want to compare on 

for indx in dummy_df.index : 

    ## checks if any row is identical, if so, drops it. 

    if ((np.array(dummy_df.loc[indx][relevant_columns]) == main_df[relevant_columns].values).sum(1) == len(relevant_columns)).sum() > 0: 

     dummy_df = dummy_df .drop(indx) 

ps:我假設數據是在熊貓數據幀格式。
希望它能幫助:)

PS2:如果頭/列有不同的名稱,它不會工作