Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I want to print a certain dictionary key using the Counter class

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading