如何根据Pandas中的条件逐行计算列的值

时间:2015-11-15 16:54:36

标签: python-2.7 pandas dataframe

我的pandas数据帧df低于

df = pd.DataFrame({'id':[1,2,3],'v' : ['r','r','i'], 'w' : ['r','r','i'],'x' : ['r','i','i']})
df

id v  w  x
1  r  r  r
2  r  r  i
3  i  i  i

列的值为ri。我想逐行计算ri的出现次数,并生成另外两个列标题ri,计数为r和i`为每行的值,我期待的最终结果如下所示

id v  w  x r i
1  r  r  r 3 0
2  i  r  r 2 1
3  i  i  i 0 3

1 个答案:

答案 0 :(得分:2)

方法1

In [15]:
def count(df):
    df['i'] = np.sum(df == 'i')
    df['r'] = np.sum(df == 'r')
    return df

In [16]:
df.apply(count, axis = 1)
Out[16]:
    id  v   w   x   i   r
0   1   r   r   r   0   3
1   2   r   r   i   1   2
2   3   i   i   i   3   0

方法2

In [9]:
count = df.apply(lambda x : x.value_counts() , axis = 1)[['i' , 'r']]
count
Out[9]:
    i   r
0   NaN 3
1   1   2
2   3   NaN

In [10]:
pd.concat([df , count.fillna(0)] , axis = 1)
Out[10]:
    id  v   w   x   i   r
0   1   r   r   r   0   3
1   2   r   r   i   1   2
2   3   i   i   i   3   0