我的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
列的值为r
和i
。我想逐行计算r
和i
的出现次数,并生成另外两个列标题r
和i
,计数为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
答案 0 :(得分:2)
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
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