python - pandas 字符串列的交集/子集

标签 python pandas networkx graph-theory intersection

我有一个 pandas 数据框架,其付款结构如下:

>> print(df)

id      time      amount      seller     buyer
-------------------------------------------------
1       07:01     16.00       Jack       Rose
2       07:03     14.00       Alice      Bob
3       07:05     95.00       Jim        Larry
...     ...       ...         ...        ...
9999    18:16     81.00       Rose       Alice

如何从中找到“封闭成员(member)”支付网络?

例如,如果我想查找仅包含 {Rose, Alice, Jim} 彼此之间严格付款的数据子集,则以下方法可能有效:

members = ['Rose', 'Alice', 'Jim']
df_subset = df[df.seller.isin(members) & df.buyer.isin(members)]

但是如何检索最大的此类网络?即不仅针对 3 个人,而且还针对数据框中的最大可能人数?

我已经尝试过以下变体:

df_subset = df[df.seller.isin(df.buyer.unique())]
df_subset = df_subset[df_subset.buyer.isin(df_subset.seller.unique())]

但是,这并不成功,因为之后 df_subset.seller.unique()df_subset.buyer.unique() 并不相同。

如有任何帮助,我们将不胜感激。

我相信最终 df_subset.seller.unique()df_subset.buyer.unique() 应该是相同的。

最佳答案

这就是你在最大人数中寻找的东西

a = df[df.seller].drop_duplicates()
b = df[df.buyer].drop_duplicates()
result = pd.concat([a,b])

关于python - pandas 字符串列的交集/子集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56532937/

相关文章:

python - Pandas 数据框的高效展平

python - 如何分析毒性?

python - 如何在python中将列表列表转换为数据框

python - pandas groupby 时间序列数据根据函数结果

python - 图中的节点未显示正确的标签

python - 具有大对象的 Memcached(NetworkX 图)

python - Pytube 错误 : AttributeError: 'module' object has no attribute 'Client'

python - Tensorflow.image.decode_jpeg 在解码图像 TFRecord 数据时移动值

python - 从 Pandas 数据框中删除停用词

python - 读取二部图