Search code examples
pythonstringpandasdataframecontains

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


Let's take this small dataframe :

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

I would like to create a new dataframe :
- Having its index and column names equal to the values of column Name
- Whose values are equal to true or false if the index belongs to the column name

Expected output :

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

How please could I do ?


Solution

  • Use 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