如何找到python pandas中的价值的关系? 首先,对我的英语不好。 我的问题是:我想通过使用语言python pandas的类似销售地点找到可以在一个地址一起出售的产品。为e ...

问题描述 投票:0回答:0

产品

销售位置

BcdeC,e
a 1、2、3
2,5
7,8,9,10
5,4
10,11
recult是: a,b,d
季节:

产品A和B具有相同的相似销售地点为2;产品B和D具有相同的相似销售地点为5。因此,可以在一个地址中一起出售A,B,D。

与产品C和E.

相似

    脚本是:
  • import pandas as pd import networkx as nx data = { "Product": ["A", "B", "C", "D", "E"], "Selling_Locations": [[1, 2, 3], [2, 5], [7, 8, 9, 10], [5, 4], [10, 11]] } df = pd.DataFrame(data) G = nx.Graph() for product in df["Product"]: G.add_node(product) for i in range(len(df)): for j in range(i + 1, len(df)): if set(df["Selling_Locations"][i]) & set(df["Selling_Locations"][j]): G.add_edge(df["Product"][i], df["Product"][j]) groups = list(nx.connected_components(G)) for i, group in enumerate(groups, 1): print(f"Group {i}: {sorted(group)}")
python pandas dataframe
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.