Skip to content

Instantly share code, notes, and snippets.

@Paulooh007
Created March 7, 2020 20:20
Show Gist options
  • Save Paulooh007/756d89416891c84338192354a6eae37f to your computer and use it in GitHub Desktop.
Save Paulooh007/756d89416891c84338192354a6eae37f to your computer and use it in GitHub Desktop.
feat_eng
# The first step is to convert the data type of the column (TRANSACTION TIME) to DateTime.
# That can be done using .to_datetime() method in Pandas.
train['transaction time']= pd.to_datetime(train['transaction time'])
train['hour'] = train['transaction time'].dt.hour #returns the hour of the datetime(range 1 - 24)
train['dayofweek_name'] = train['transaction time'].dt.weekday_name #day of week of transaction
train['is_weekend'] = np.where(train['dayofweek_name'].isin(['Sunday','Saturday']),1,0) #weekend or not
#Getting the period of the day from the hour feature
bins = [0,4,8,12,16,20,24]
period = ['Late Night', 'Early Morning','Morning','Noon','Eve','Night']
train['period_of_day'] = pd.cut(train['hour'], bins=bins, labels=period)
#Getting the recent amount withdrawn or deposited
train['prev_tran'] = train['current bank amount'] - train['last bank amount']
#credit if the value is positive or debit if otherwise
train['credit_or_debit'] = ['credit' if x > 0 else 'debit' for x in train['prev_tran'] ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment