jeudi 1 février 2018

How to create a feature in python based on a function?

first time stack overflow-er here. I'm trying to create some new features for this data science project that I'm working on (categorizing bank transaction data). For instance, I have transaction categories like "Auto Payments", "Gas", "Parking", etc. and I want to create a new feature called "Car" where any category with any of those words will be given a value of 1 (and all other categories will be given a 0).

I tried to write a function for "car", but I can't figure out how to create a new feature based on the results of this function. I feel like there must be a simple way to do this, but I'm a bit new at python and having trouble figuring it out!

Here's my function for car:

def car():
    df.category.apply(lambda x: "Auto" in x)
    df.category.apply(lambda x: "Gas" in x)
    df.category.apply(lambda x: "Parking" in x)
    df.category.apply(lambda x: "Service & Parts" in x)

Here's the code for how I tried to create a new column of "car":

df['car'] = "1" if car==True else "0"

Here's what the df looks like:

Date Description    Amount  Category

2017-01-02  Tacos   8.34    Restaurants
2017-01-02  Transfer to Free Checking   800.00  Transfer
2016-12-30  Shell   26.26   Gas & Fuel
2016-12-29  Sunoco  22.59   Gas & Fuel

Thanks! I appreciate any input that you might have.

Aucun commentaire:

Enregistrer un commentaire