Skip to content

Instantly share code, notes, and snippets.

View gnurio's full-sized avatar

George Nurijanian gnurio

View GitHub Profile
@gnurio
gnurio / decode_encode_onehot
Created February 22, 2018 22:49
Re-coding a categorical field into one-hot vectors
import pandas as pd
from sklearn.preprocessing import LabelEncoder,OneHotEncoder
def decode_encode(colname):
'''
(str) -> (DataFrame)
Returns a Data Frame with the column given to it transformed into a One-hot encoded set of columns
@gnurio
gnurio / gist:c3bd6d5fa9d67880014e5660df1368f8
Created March 21, 2017 23:43
How to calculate the mode for different categories in a DataFrame
import pandas as pd
from scipy.stats import mode
grouped_mod = df.pivot_table(values='The metric you need the mode for',columns='Category',aggfunc=mode)