Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在特定表的特定列中查找公共值并显示相交输出_Python_Python 2.7_Pandas - Fatal编程技术网

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)