I have a code that returns ‘True’ if the value is equal to the one before it and ‘False’ if it is not equal. I am now trying to use the Counter class to print out the sum of the values that returned False. However, I keep getting all dictionaries/keys returned instead of just the values associated with ‘False’.
My code:
df['cols2'] = df['cols1'].rolling(2).apply(lambda x: len(set(x)) != len(x),raw= True).replace({0 : False, 1: True})
p = (df['cols2'] , Counter['False'])
print (p)
>Solution :
Did you try this :
from collections import Counter
import pandas as pd
# Assuming you have already defined df and calculated 'cols2' using your previous code
# Create a Counter object from 'cols2'
counter_obj = Counter(df['cols2'])
# Access the count of occurrences of 'False' in the Counter object
false_count = counter_obj[False]
# Print the count
print("Count of 'False' in cols2:", false_count)