我为数据设置了二进制变量(1或0)。 我想创建一个新变量,如果特定组的变量(在下面的示例中为X2,X3,X4)中的任何一个为1,则该变量的值为1 否则为0。
数据说明:
<html>
<body>
<p>Before the script...</p>
<!-- This element exists in your DOM tree already so you
don't really need the document to load. -->
<script>
alert( 'Hello, world!' );
</script>
<p>...After the script.</p>
<!-- This element is added after the script so if you run
the code above trying to find this "p" tag you would need
to add a proper window.onload event -->
</body>
</html>
预期结果:
X1 X2 X3 X4
34 0 0 0
26 0 1 1
89 1 0 0
答案 0 :(得分:1)
通过DataFrame.any
每行至少测试一个1
并转换为integer
:
df['new'] = df[['X2', 'X3', 'X4']].any(axis=1).astype(int)
或使用Series.view
:
df['new'] = df[['X2', 'X3', 'X4']].any(axis=1).view('i1')
df['new'] = np.where(df[['X2', 'X3', 'X4']].any(axis=1), 1, 0)
print (df)
X1 X2 X3 X4 new
0 34 0 0 0 0
1 26 0 1 1 1
2 89 1 0 0 1