Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count no of rows in a data frame whose values divisible by 3 or 5?

I have a data frame with two columns:

 ones   zeros
0   6   13
1   8   7
2   11  7
3   8   5
4   11  5
5   10  6
6   11  6
7   7   4
8   9   4
9   4   6
10  7   5
11  6   7
12  9   10
13  14  3
14  7   7
15  7   7
16  9   7
17  7   10
18  9   5
19  12  7
20  4   8
21  6   4
22  11  5
23  9   7
24  3   10
25  7   4
26  6   12
27  9   7
28  7   4
29  9   9
... ... ...
4339    10  9
4340    7   10
4341    6   11
4342    4   6
4343    9   11
4344    5   11
4345    7   9
4346    9   5
4347    11  7
4348    9   10
4349    8   10
4350    6   5
4351    5   8
4352    5   7
4353    5   8
4354    7   13
4355    11  3
4356    6   7
4357    7   6
4358    8   12
4359    8   11
4360    7   11
4361    6   13
4362    8   3
4363    11  8
4364    9   3
4365    6   5
4366    9   6
4367    11  8
4368    4   3 

I need to count no of rows where numbers under column ones are divisible by 5 or numbers under column zeros are divisible by 3.

As this is sub-section of a problem. I have prepared the data frame after cleaning and the data frame having 2 columns and 4369 no of rows.

I have tried this one, but this is not the solution for the condition "OR" rather it is the solution for "AND"

score['count_zeros'].value_counts(dropna=False)
score['count_ones'].value_counts(dropna=False)

I need the solution for OR condition

like image 379
Kirti Swagat Mohanty Avatar asked Sep 24 '19 10:09

Kirti Swagat Mohanty


Video Answer


1 Answers

Compare modulo 5 and 3 with 0 and filter by boolean indexing with | for bitwise OR:

df = df[(df['ones'] % 5 == 0) | (df['zeros'] % 3 == 0)]
print (df)
      ones  zeros
5       10      6
6       11      6
9        4      6
13      14      3
26       6     12
29       9      9
4339    10      9
4342     4      6
4344     5     11
4345     7      9
4351     5      8
4352     5      7
4353     5      8
4355    11      3
4357     7      6
4358     8     12
4362     8      3
4364     9      3
4366     9      6
4368     4      3

If need to count number of matched values:

out = ((df['ones'] % 5 == 0) | (df['zeros'] % 3 == 0)).sum()
print (out)
20
like image 72
jezrael Avatar answered Oct 01 '22 10:10

jezrael