Pandas: count some value in all columns

I have a dataframe

graph   0       1       2       3       4
1       blue    blue    blue    blue    blue
2       blue    blue    blue    blue    blue
3       blue    red     blue    blue    red
4       red     blue    red     red     blue
5       red     red     blue    red     red
6       blue    blue    blue    blue    blue

I need to get a blue counter for each row / row.
Desired Result:

graph   result
1       5
2       5
3       3
4       2
5       1
6       5

I am trying to do this with

(df['0', '1', '2', '3', '4']).applymap(lambda x: str.count(x, 'blue'))

But he returns

KeyError: ('0', '1', '2', '3', '4')
+4
source share
2 answers
In [35]: df.set_index('graph').eq('blue').sum(1).reset_index(name='result')
Out[35]:
   graph  result
0      1       5
1      2       5
2      3       3
3      4       2
4      5       1
5      6       5
+3
source

With numpybent. Restore from scratch if you reliably know where the column is graph, namely the column 0.

v = df.values
pd.DataFrame(dict(graph=v[:, 0], result=(df.values[:, 1:] == 'blue').sum(1)))

  graph  result
0     1       5
1     2       5
2     3       3
3     4       2
4     5       1
5     6       5

naive time test
enter image description here

+1
source

Source: https://habr.com/ru/post/1673831/


All Articles