python 我想使用Counter类打印某个字典键

0ejtzxu1  于 2023-04-19  发布在  Python
关注(0)|答案(1)|浏览(78)

我有一个代码,如果值等于它之前的值,则返回'True',如果不等于,则返回'False'。我现在试图使用Counter类来打印出返回False的值的总和。然而,我一直得到所有字典/键的返回,而不仅仅是与'False'相关的值。
我的代码:

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)
gab6jxml

gab6jxml1#

你试过这个吗:

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)

相关问题