2015-10-20 21 views
2

如何找到在某特定表的特定列的共同價值觀,並顯示相交輸出

Roll Class Country Rights CountryAcc 
1  x  IND  23  US 
1  x1  IND  32  Ind 
2  s  US  12  US 
3  q  IRL  33  CA 
4  a  PAK  12  PAK 
4  e  PAK  12  IND 
5  f  US  21  CA 
5  g  US  31  PAK 
6  h  US  21  BAN 

我想只顯示那些RollsCountryAccUSCA。例如:如果Roll1US一個CountryAcc然後我不希望它的其他一行CountryAccInd和同去與Roll5,因爲它是具有一行與CountryAccCA。所以,我最後的結果將是:

Roll Class Country Rights CountryAcc 
4  a  PAK  12  PAK 
4  e  PAK  12  IND 
6  h  US  21  BAN 

我試圖讓下面的方式輸出:

Home_Country = ['US', 'CA'] 

#First I saved two countries in a variable 
Account_Other_Count = df.loc[~df.CountryAcc.isin(Home_Country)] 
Account_Other_Count_Var = df.loc[~df.CountryAcc.isin(Home_Country)][['Roll']].values.ravel() 

# Then I made two variables one with CountryAcc in US or CA and other variable with remaining and I got their Roll 
Account_Home_Count = df.loc[df.CountryAcc.isin(Home_Country)] 
Account_Home_Count_Var = df.loc[df.CountryAcc.isin(Home_Country)][['Roll']].values.ravel() 

#Here I got the common Rolls 
Common_ROLL = list(set(Account_Home_Count_Var).intersection(list(Account_Other_Count_Var))) 
Final_Output = Account_Other_Count.loc[~Account_Other_Count.Roll.isin(Common_ROLL)] 

有沒有更好的,更大熊貓或Python的方式來做到這一點。

+0

@EdChum你能在這裏如果可能的幫助。謝謝 –

+0

請不要發送個人要求幫助我不是你的奴隸 – EdChum

+0

嘿,對不起,但那是苛刻的。 @EdChum –

回答

0

一種解決方案可能是

In [37]: df.ix[~df['Roll'].isin(df.ix[df['CountryAcc'].isin(['US', 'CA']), 'Roll'])] 
Out[37]: 
    Roll Class Country Rights CountryAcc 
4  4  a  PAK  12  PAK 
5  4  e  PAK  12  IND 
8  6  h  US  21  BAN 
+0

謝謝約翰。如果可能的話,你可否請解釋一下。 –

0

這是做這件事:

sortdata = df[~df['CountryAcc'].isin(['US', 'CA'])].sort(axis=0) 
+0

我得到的卷有兩個CountryAcc,其中一個也是美國。我得到這樣的行例如:'1 x1 IND 32 Ind' –

相關問題