mardi 23 juin 2020

Create column input based on string input in Python function

So I am writing a custom function where I want a string input being transformed into a dataframe, ready for a model to do predictions on. Right now I am stuck writing a piece of code that is able to detect if the input string contains specific words and then if it does, process this to a dataframe column where the value is either 1 or 0. However, I have not been able to succesfully wright it, how can I best approach this? This is my piece of code:

def function(text_input)
    cols = ['col_foo', 'col_bar', 'col_hello', 'col_world']
    words = ['foo', 'bar', 'hello', 'world']
    data = [text_input.split()]
    for col in cols:
        df[col] = 0
    for word in data:
        if word in words:
           for words in cols:
               df[cols] = 1
    return df

I am expecting that all cols will first be created with initial 0 values. Then based on the words in the text_input that match with the words in the words list, the corresponding cols will be set to 1 values. But right now, this code outputs all df[cols] = 0

What am I doing wrong in this piece of code? Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire