[英]Python Generate dummy in dataframe based on another variable(pandas)
I have dataframe with many variables.我有很多变量的数据框。 I would like to generate a dummy variable based on column 1, for example.例如,我想根据第 1 列生成一个虚拟变量。 If column 1's observation is more than 0.25 then the dummy variable is filled with 1. If column 1' observation is less than 0.25, then the dummy variable is filled with 0. Any ideas?如果第 1 列的观察值大于 0.25,则虚拟变量填充为 1。如果第 1 列的观察值小于 0.25,则填充虚拟变量为 0。有什么想法吗? Thanks a lot.非常感谢。
IIUC compare values for greater like 0.25
and then cast to integers for map True/False
to 1/0
: IIUC 比较大于0.25
值,然后转换为整数以将True/False
映射到1/0
:
df[1] = df[1].gt(0.25).astype(int)
If first column:如果第一列:
df.iloc[:, 0] = df.iloc[:, 0].gt(0.25).astype(int)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.