我试图计算我的数据帧中每种类型的行的重复项.例如,假设我在pandas中有一个数据帧,如下所示:
df = pd.DataFrame({'one': pd.Series([1., 1, 1]),
'two': pd.Series([1., 2., 1])})
我得到一个看起来像这样的df:
one two
0 1 1
1 1 2
2 1 1
我想第一步是找到所有不同的唯一行,我这样做:
df.drop_duplicates()
这给了我以下df:
one two
0 1 1
1 1 2
现在我想从上面的df([1 1]和[1 2])中获取每一行,并计算每个在初始df中的次数.我的结果看起来像这样:
Row Count
[1 1] 2
[1 2] 1
我该怎么办呢?
编辑:
这是一个更大的例子,使其更清晰:
df = pd.DataFrame({'one': pd.Series([True, True, True, False]),
'two': pd.Series([True, False, False, True]),
'three': pd.Series([True, False, False, False])})
给我:
one three two
0 True True True
1 True False False
2 True False False
3 False False True
我想要一个告诉我的结果:
Row Count
[True True True] 1
[True False False] 2
[False False True] 1
解决方法:
您可以对所有列和调用大小进行分组,索引指示重复值:
In [28]:
df.groupby(df.columns.tolist(),as_index=False).size()
Out[28]:
one three two
False False True 1
True False False 2
True True 1
dtype: int64
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。