Answers for "dummy variable pandas"

6

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)
Posted by: Guest on May-11-2020
0

how to get dummies in a dataframe pandas

df = pd.get_dummies(df, columns=['type'])
Posted by: Guest on September-03-2020
0

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)
Posted by: Guest on November-27-2020
0

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
Posted by: Guest on November-27-2020

Python Answers by Framework

Browse Popular Code Answers by Language