简体   繁体   English

Pandas DataFrame行明智比较

[英]Pandas DataFrame row wise comparison

I have a pandas DataFrame like following. 我有一个像下面这样的pandas DataFrame

       id  label_x label_y
0        1    F    R
1        2    F    F
2        3    F    F
3        4    F    F
4        5    F    F

Now I want to count occurrences of label_x and label_y are equal and not equal. 现在我想计算label_x和label_y的出现次数是否相等而不相等。 In this case there is only one occurrence of not equal and 4 occurrences of equal. 在这种情况下,只有一次出现不相等且出现次数相等。

df = pd.DataFrame({'id' : ["1","2","3","4","5"],
                'label_x'  : ["F","F","F","F","F"], 'label_y' : ["R","F","F","F","F"]})
(df.label_x == df.label_y).value_counts()

Many ways to to that, including the above... 许多方法,包括上述......

In [43]: (df.label_x == df.label_y).value_counts()
Out[43]:
True     4
False    1
dtype: int64

I came up with this solution. 我想出了这个解决方案。 Is that the best one? 这是最好的吗?

def compare(x):
    if x[1] == x[2]:
        return 'yes'
    else:
        return 'no'

df['result'] =  df.apply(compare, axis=1)

df2 = pd.DataFrame({'count' : df.groupby( ["result"] ).size()}).reset_index()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM