简体   繁体   English

如何在 pandas 的 2 列 dataframe 中找到唯一组合的计数

[英]how do I find count of unique combination in 2 columns of dataframe in pandas

df = pd.DataFrame({'col1': [1,2,4,3], 'col2': [2,1,3,4]})
   col1 col2
0   1     2
1   2     1
2   4     3
3   3     4

Desired outcome期望的结果

  col1 col2 count
0   1     2     2
1   4     3     2

I tried我试过了

(df.groupby(['team1','team2']).size()
   .sort_values(ascending=False)
   .reset_index(name='Count')
)

but this is not giving me unique combination但这并没有给我独特的组合

IIUC, you can first compute afrozenset from your two columns, then use named aggregation : frozenset ,您可以先从两列计算冻结集,然后使用命名aggregation

# compute unordered grouper
group = df[['col1', 'col2']].agg(frozenset, axis=1)

# craft a dictionary of expected output
# first rows for the existing columns + new column for count
d = {c: (c, 'first') for c in df}
d.update({'count': ('col1', 'count')})
# {'col1': ('col1', 'first'),
#  'col2': ('col2', 'first'),
#  'count': ('col1', 'count')}

# perform the aggregation
df.groupby(group, as_index=False).agg(**d)

output: output:

   col1  col2  count
0     1     2      2
1     4     3      2

You do something like this also,你也做这样的事情,

df.apply(set, axis=1).value_counts()

Output: Output:

{1, 2}    2
{3, 4}    2
dtype: int64

Let us check让我们检查一下

df[:] = np.sort(df.to_numpy(),axis=1)
df.value_counts()
Out[132]: 
col1  col2
1     2       2
3     4       2
dtype: int64

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 pandas dataframe 列拆分为 3 个唯一列? - How do I split a pandas dataframe column into 3 unique columns? 如何过滤和分组 pandas DataFrame 以获得两列组合的计数 - How to filter and group pandas DataFrame to get count for combination of two columns 如何根据 pandas 中其他两列的唯一组合获得两列的唯一计数 - How to get unique count of two columns based on unique combination of other two columns in pandas python-我如何从 pandas dataframe 中删除 2 列值的行(这些值应该是 2 个字符串的组合)? - python- How do i remove a rows from pandas dataframe by 2 columns value (The values should be a combination of 2 strings )? 如何查找数据框的唯一对值(在不同的行和列上)的计数并在 Python 中进行可视化? - How to find count of unique pair values (on different rows and columns) of a dataframe and do its visualization in Python? 如何在 DataFrame 中找到重复组合的频率计数 - How can i find the count of freuency of repeated combination in DataFrame 将唯一 ID 分配给 Pandas 数据框中两列的组合,按其顺序独立 - Assign unique ID to combination of two columns in pandas dataframe independently on their order 我如何找到 pandas 中列组合的百分比 - how can i find percentage of the combination of the columns in pandas 如何对 Pandas 数据框进行二分搜索以获取列值的组合? - How do I binary search a pandas dataframe for a combination of column values? 如何计算 Pandas DataFrame? - How do I count in a Pandas DataFrame?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM