Python 如何在特定表的特定列中查找公共值并显示相交输出
桌子 我只想显示其Python 如何在特定表的特定列中查找公共值并显示相交输出,python,python-2.7,pandas,Python,Python 2.7,Pandas,桌子 我只想显示其CountryAcc不在US或CA的Rolls。例如:如果Roll1在US中有一行CountryAcc,那么我不希望它的另一行带有CountryAccInd和Roll5,因为它有一行带有CountryAcc和CA。因此,我的最终输出将是: Roll Class Country Rights CountryAcc 1 x IND 23 US 1 x1 IND 32 Ind 2 s
CountryAcc
不在US
或CA
的Rolls
。例如:如果Roll
1
在US
中有一行CountryAcc
,那么我不希望它的另一行带有CountryAcc
Ind
和Roll
5
,因为它有一行带有CountryAcc
和CA
。因此,我的最终输出将是:
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
我尝试通过以下方式获得该输出:
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)]
这是一种方法:
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
@如果可能的话,EdChum你能在这里帮忙吗。谢谢不要发送个人求助请求我不是你的奴隶抱歉,但那太苛刻了@EdChumSorry,但你已经多次向用户发送ping消息,要求他们就你之前的一些问题提供帮助,正如我之前所说的那样,这不是一个论坛,也不是供你个人请求帮助的论坛help@EdChum我们可以在某个网站上聊天吗。你能帮我一下吗,或者给我一些建议。很抱歉打扰你,但我没有认识熊猫的朋友。我买的面包卷有两个国家,其中一个也是我们。我得到那一排,例如:
1x1ind32ind
谢谢约翰。如果可能的话,你能解释一下吗。
sortdata = df[~df['CountryAcc'].isin(['US', 'CA'])].sort(axis=0)