pandas dummy variables no prefix code example
Example 1: getting dummies and input them to pandas dataframe
note:
dummies = pd.get_dummies(df[['column_1']], drop_first=True)
df = pd.concat([df.drop(['column_1'],axis=1), dummies],axis=1)
note:for more that one coloum keep ading in the list
dummies = pd.get_dummies(df[['column_1', 'column_2','column_3']], drop_first=True)
df = pd.concat([df.drop(['column_1', 'column_1'],axis=1), dummies],axis=1)
Example 2: how to get dummies in a dataframe pandas
df = pd.get_dummies(df, columns=['type'])
Example 3: pandas bins dummy
>>> dout
age ageD_[5, 30] ageD_(30, 70] (-inf, 5] (-inf, 30] (-inf, 70] (5, +inf) (30, +inf) (70, +inf)
0 5 1 0 1 1 1 0 0 0
1 23 1 0 0 1 1 1 0 0
2 43 0 1 0 0 1 1 1 0
3 70 0 1 0 0 1 1 1 0
4 30 1 0 0 1 1 1 0 0
Example 4: pandas bins dummy
df['ageD'], bins = pd.qcut(df.iloc[:, 0], 2, retbins=True)
left = (df["age"].values <= bins[:,None]).T.astype(int)
dl = pd.DataFrame(left, columns=["(-inf, {}]".format(b) for b in bins])
dr = pd.DataFrame(1-left, columns=["({}, +inf)".format(b) for b in bins])
dout = pd.concat([pd.get_dummies(df), dl, dr], axis=1)