如何让字符串列的所有包含匹配项?

How to have all the contain matches of a string column?

让我们来看看这个小数据框:

df = pd.DataFrame(dict(Name=['abc','abcd','bc']))
   Name
0   abc
1  abcd
2    bc

我想创建一个新的数据框:
- 使其索引和列名称等于列名称
的值 - 如果索引属于列名

,则其值等于true或false

预期输出:

      abc   abcd  bc
abc   True  True  False
abcd  False True  False
bc    True  True  True

请问我该怎么办?

使用Series.str.contains in list comprehension, create masks and join together by concat, then set index, transpose by DataFrame.T and last remove index and columns names by DataFrame.rename_axis:

s = df['Name']
L = [s.str.contains(x) for x in s]
df = pd.concat(L, axis=1, keys=s).set_index(s).T.rename_axis(index=None, columns=None)
print (df)
        abc  abcd     bc
abc    True  True  False
abcd  False  True  False
bc     True  True   True