Answers for "pandas bins dummy"

1

pd.cut in pandas

>>> pd.qcut(range(5), 4, labels=False)
array([0, 0, 1, 2, 3])
Posted by: Guest on October-20-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
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

Python Answers by Framework

Browse Popular Code Answers by Language