一旦你
count duplicate rows
,只需要做一些工作就可以得到相应的标签。
以下是我将如何做到这一点,尽管我对熊猫不太熟悉,所以可能有更好的方法。首先,df应该是布尔值。
import pandas as pd
df = pd.DataFrame({
'AA': [1, 0, 0, 1, 1],
'BB': [0, 0, 1, 0, 0],
'CC': [1, 1, 0, 1, 1]}
).astype(bool)
# Count duplicate rows
counts = df.groupby(df.columns.tolist()).size()
# Get most common rows
maxima = counts[counts==counts.max()]
for combination, count in maxima.iteritems():
# Select matching labels
labels = df.columns[list(combination)]
print(*labels, count)
输出:
AA CC 3
部分结果:
>>> counts
AA BB CC
False False True 1
True False 1
True False True 3
dtype: int64
>>> maxima
AA BB CC
True False True 3
dtype: int64