Pandas计算跨行的值,这些值大于另一列中的另一个值

时间:2018-04-20 00:27:07

标签: python pandas numpy count rows

我有一个像这样的pandas数据框:

        X   a   b   c
        1   1   0   2
        5   4   7   3
        6   7   8   9

我想打印一个名为' count'的列。它输出的值的数量大于第一列中的值(在我的情况下为' x')。输出应如下所示:

X   a   b   c   Count
1   1   0   2   2
5   4   7   3   1
6   7   8   9   3

我想不要使用' lambda函数'或者' '循环或任何类型的循环技术,因为我的数据帧有大量的行。我试过这样的事情,但我无法得到我想要的东西。

df['count']=df [ df.iloc [:,1:] > df.iloc [:,0] ].count(axis=1)

我也试过

numpy.where()

也没有运气。所以任何帮助将不胜感激。我也有nan作为我的数据帧的一部分。所以当我计算价值时,我想忽略它。

提前感谢您的帮助!

2 个答案:

答案 0 :(得分:3)

您可以将ge(> =)与sum

一起使用
df.iloc[:,1:].ge(df.iloc[:,0],axis = 0).sum(axis = 1)
Out[784]: 
0    2
1    1
2    3
dtype: int64

分配后

df['Count']=df.iloc[:,1:].ge(df.iloc [:,0],axis=0).sum(axis=1)
df
Out[786]: 
   X  a  b  c  Count
0  1  1  0  2      2
1  5  4  7  3      1
2  6  7  8  9      3

答案 1 :(得分:1)

df['count']=(df.iloc[:,2:5].le(df.iloc[:,0],axis=0).sum(axis=1) + df.iloc[:,2:5].ge(df.iloc[:,1],axis=0).sum(axis=1))

In case anyone needs such a solution, you can just add the output you get from '.le' and '.ge' in one line. Thanks to @Wen for the answer to my question though!!!

相关问题