Search code examples
python-3.xnlpsklearn-pandas

read word from each row in a dataframe


I want to read the word "risk" from every row in dataframe. If a row have the word risk in it then the dataframe should make a new column which will put 1 in it else 0. How can I achieve this ?


Solution

  • Not sure of a pythonic way. but i think this is what youre looking for

    import pandas as pd
    data = ['hello risk', 'hello there', 'hi']
    df = pd.DataFrame({'Sample data':data})
    
    
    findrisk = [str(i).find("risk") for i in df['Sample data']]
    check=[]
    for i in findrisk:
        if i >= 0:
            i = 1
        else:
            i = 0
        check.append(i)
    
    df['result'] = check