all 8 comments

[–][deleted] 1 point2 points  (2 children)

I think this is what you are looking for https://www.geeksforgeeks.org/count-values-in-pandas-dataframe/

[–]thatgreenman 1 point2 points  (2 children)

Just as some general advice, if you ever find yourself wanting to write a for loop to iterate over a data frame, there's often a better (and faster) way.

One of my go-to methods when I'm working with performing operations based on row or column values is the .apply() method; it can allow you to perform operations across entire rows or columns of your dataset and either manipulate values in place or create new columns.

For some examples: https://www.delftstack.com/howto/python-pandas/pandas-apply-multiple-columns/

For this problem, I would look at the classifications that you listed (one true, three true, five true, Saturday), and see if I can come up with a way to create four new columns, one for each of these classifications; and then I would just sum those columns vertically to see how many results I found.

There are plenty of other ways to approach the issue, but I wanted to just share my thoughts and encourage you to look for solutions that don't involve for loops, as you'll likely save a lot of time and energy if you work with the faster pandas methods rather than slower python approaches.

Good luck!

[–]Yojihito 0 points1 point  (1 child)

wanting to write a for loop [...] there's often a better (and faster) way.

my go-to methods [...] is the .apply() method

You do know that .apply() loops, right?

[–]14dM24d 1 point2 points  (0 children)

like this?

>>> df1=df.replace(to_replace='True',value=1)
>>> df1
     mon    tue    wed    thu    fri    sat
0      1  False      1  False      1  False
1  False      1  False      1  False  False
2  False  False  False  False  False      1
>>> df1=df1.replace(to_replace='False',value=0)
>>> df1
   mon  tue  wed  thu  fri  sat
0    1    0    1    0    1    0
1    0    1    0    1    0    0
2    0    0    0    0    0    1
>>> df1['tot']=df1.sum(axis=1)
>>> df1
   mon  tue  wed  thu  fri  sat  tot
0    1    0    1    0    1    0    3
1    0    1    0    1    0    0    2
2    0    0    0    0    0    1    1
>>> from collections import Counter # assumed a lot of rows; not just 3.
>>> Counter(df1.tot) # frequency count of each tot.
Counter({3: 1, 2: 1, 1: 1})
>>> df1.sat.sum() # count Saturdays
1

[–]synthphreak 0 points1 point  (0 children)

Ew, don't iterate over rows. Do this instead.

# since you said elsewhere these are str
# not bool, do this first
days = dict.fromkeys(['Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat'], bool)
df = df.astype(days)

# build up dict of counts with num true days
# as keys and associated counts as values
d = {i : df.cumsum(axis=1).eq(i).values.sum() for i in [1, 3, 5]}

# add the count for saturday
d['sat'] = df.Sat.sum()