1
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

1
  • what you tried should be posted in the question. Comments are meant for clarifications and may get deleted. Please edit what you've tried into the question. Commented Apr 19, 2022 at 13:57

3 Answers 3

2

You do something like this also,

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

Output:

{1, 2}    2
{3, 4}    2
dtype: int64
Sign up to request clarification or add additional context in comments.

Comments

2

IIUC, you can first compute a frozenset from your two columns, then use named 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:

   col1  col2  count
0     1     2      2
1     4     3      2

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.