2016-11-21 116 views
0

与其他列的组合我有熊猫DF没有。在大熊猫DF

id_x    id_y 
    a     b 
    b     c 
    c     d 
    d     a 
    b     a 
and so on around (1000 rows) 

我想找到与id_y每个id_x组合的计数表。

即。一个具有a-b,d-a(total 2 combinations) 同样B级组合已总2 combinations(b-c) and also a-b to be considered as a combination for b(a-b = b-a)

并创建一个数据帧DF2具有

id combinations 
a   2 
b   2 
c   2 #(c-d and b-c) 
d   1 
and so on ..(distinct product_id_'s) 

我试图这样做代码

df.groupby(['id_x']).size().reset_index() 

但得到错误的结果;

id_x 0 
0 a 1 
1 b 1 
2 c 1 
3 d 1 

我应该遵循什么方法?我的python技能在初学者水平。 在此先感谢。

+0

@jezrael这是我的问题 – Shubham

回答

2

你可以先排序全部由applysorted行,然后通过stack和最后value_counts创建Series

df = df.apply(sorted,axis=1).drop_duplicates().stack().value_counts() 
print (df) 
d 2 
a 2 
b 2 
c 2 
dtype: int64 
+0

而是说,我有AB和BA,排序将BA转换成ab,因此b的计数应该是2(bc和ba)现在只有1 – Shubham

+0

@我不想组合 – Shubham

+0

是'd'' 2'吗?或不? – jezrael