Pandas:如何对列元素的组合进行分组,以指示基于不同列的值的共现性?

3
我有一个形式为df的pandas数据框。
Batch_ID    Product_ID
   1            A
   1            B
   1            C
   2            B
   2            B
   2            C
   2            C
   3            B
   3            B
   3            C
   4            C
   4            D
   5            D

我希望从中获得一个边缘列表,基本上是一个新的数据框edge_list_df(我可以将其转换为networkx对象),格式如下:
Source       Target         Weight
  A             B             1.0
  A             C             1.0
  A             D             0.0
  B             C             3.0
  B             D             0.0
  C             D             1.0

请注意,我在示例中给出了许多不同的可能性,以确保我的问题清晰明了。例如,即使对于Batch_ID = 2,B-C组合出现两次,计数器也不会增加两次。
最有效的方法是什么?
2个回答

5
这是我的看法:

from itertools import combinations

def combine(batch):
    """Combine all products within one batch into pairs"""
    return pd.Series(list(combinations(set(batch), 2)))

edges = df.groupby('Batch_ID')['Product_ID'].apply(combine).value_counts()
edges
#(B, C)    3
#(A, B)    1
#(A, C)    1
#(D, C)    1

我理解不必要的边缘可以被称为“0-发生”边。
如果您需要,您还可以将索引进一步分为源和目标:
edges = edges.reset_index()
edges = pd.concat([edges, edges['index'].apply(pd.Series)], axis=1)
edges.drop(['index'], axis=1, inplace=True)
edges.columns = 'Weight','Source','Target'
#       Weight Source Target
#0       3      B      C
#1       1      A      B
#2       1      A      C
#3       1      D      C

或者:

c = ['Source', 'Target']
L = edges.index.values.tolist()
edges = pd.DataFrame(L, columns=c).join(edges.reset_index(drop=True))

@jezrael 看起来不错。我知道最后一块代码有点糟糕。 - DYZ
1
是的,最慢的是.apply(pd.Series) - 在答案结尾处检查时间 ;) - jezrael

2

使用NetworkX API:

In [225]: G = nx.from_pandas_edgelist(df, 'Batch_ID', 'Product_ID')

In [226]: from networkx.algorithms import bipartite

In [227]: W = bipartite.weighted_projected_graph(G, df['Product_ID'].unique())

In [228]: W.edges(data=True)
Out[228]: EdgeDataView([('A', 'C', {'weight': 1}), ('A', 'B', {'weight': 1}), ('B', 'C', {'weight': 3}), ('C', 'D', {'weight': 1})])

In [229]: nx.to_pandas_edgelist(W)
Out[229]:
  source target  weight
0      A      C       1
1      A      B       1
2      B      C       3
3      C      D       1

注意:对于NetworkX版本1.x,请使用from_pandas_dataframe()to_pandas_dataframe代替from_pandas_edgelistto_pandas_edgelist


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接