Search code examples
pythonpandascontains

Column contains column


I would like to see whether in each row of my dataframe column A contains the value that is in column B.

df = pd.DataFrame({'A': ["Is it 54321?", "Is it 4321?", "Is it 321?"],
                   'B': [54321, 54321, 54321]})

I tried:

df["C"] = df["A"] .str .contains(df["B"])

I would like:

'C': [1,0,0]

But I got:

TypeError: 'Series' objects are mutable, thus they cannot be hashed

Solution

  • I had accepted various answers on this thread but I was having problems with them, as described here: Column contains column 1

    Thanks to Wen-Ben for this answer:

    If you do want 12 to be in 123:

    df = df.dropna()
    df['C'] = [str(y) in x for x , y in zip(df.A,df.B)]
    print(df)
    

    Or if you do not want 12 to be in 123:

    df = df.dropna()
    df['C'] = [str(y) in x for x , y in zip(df.A.str.split(' '),df.B)]
    print(df)