【发布时间】:2019-10-29 15:17:35
【问题描述】:
我有一个如下的数据框:
name teamA teamB
foo a b
foo b c
foo c b
bar a e
bar a d
...
我想分别为每个名称查找行的交集,但对于列 teamA 和 teamB。然后删除包含该交集值的单元格的值。 在此示例中,对于名称“foo”,行的交集将是“b”,对于名称“bar”,将是“a”。 因此删除此交集值后的数据框将如下所示:
name teamA teamB
foo a " "
foo " " c
foo c " "
bar " " e
bar " " d
...
最近,我尝试将 teamA 和 teamB 作为以示例团队命名的列。
name teams
foo [a, b]
foo [b, c]
foo [c, b]
...
以后我想得到
name teams
foo [a, " "]
foo [" ", c]
foo [c, " "]
...
但我发现更建议将它分成两列,我发现答案很有趣,但我不知道如何将它应用于分组数据框。 https://stackoverflow.com/a/55554709/9168586(查看“在许多列上过滤”部分和“保留至少一列为真的行”)。 就像那个例子:
dataframe[['teamA', 'teamB']].isin('b').any(axis=1)
0 True
1 True
2 True
3 True
dtype: bool
其中“b”将是我将迭代的值(团队)之一。每次迭代后,如果整列为 True,我将从每行中的列 teamA 或 teamB 中删除该值并继续到另一个组。
我得到的错误是:
Cannot access callable attribute 'isin' of 'DataFrameGroupBy' objects, try using the 'apply' method
和
only list-like or dict-like objects are allowed to be passed to DataFrame.isin(), you passed a 'str'
【问题讨论】: