[英]How do I split a pandas dataframe column into 3 unique columns?
[英]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
期望的結果
col1 col2 count
0 1 2 2
1 4 3 2
我試過了
(df.groupby(['team1','team2']).size()
.sort_values(ascending=False)
.reset_index(name='Count')
)
但這並沒有給我獨特的組合
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:
col1 col2 count
0 1 2 2
1 4 3 2
你也做這樣的事情,
df.apply(set, axis=1).value_counts()
Output:
{1, 2} 2
{3, 4} 2
dtype: int64
讓我們檢查一下
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.