[英]A new column in pandas that depends on values of the other rows
I have an example data as:我有一个示例数据:
column1 column2 column3 column4
0. 1. 1. 0
1. 1. 1. 1
0. 0. 0. 0
1. 1. 1. 0
1. 1. 1. 1
I would like to create a new column(output) which shows 1 if at least one of the row values of the dataframe is 1, and 0 if the rows are all 0.我想创建一个新列(输出),如果 dataframe 的至少一个行值为 1,则显示 1,如果行全为 0,则显示 0。
The output should look like this: output 应如下所示:
column1 column2 column3 column4. output
0. 1. 1. 0. 1
1. 1. 1. 1. 1
0. 0. 0. 0. 0
1. 1. 1. 0. 1
1. 1. 1. 1. 1
Use DataFrame.any
for test if match at least one match:使用DataFrame.any
测试是否匹配至少一个匹配:
df['output'] = df.eq(1).any(axis=1).astype(int)
#alternative
df['output'] = np.where(df.eq(1).any(axis=1), 1, 0)
#if only 0,1 values is possible use
#df['output'] = df.any(axis=1).astype(int)
Use:利用:
df['output'] = np.any(df.eq(1)., axis = 1).astype(int)
You can check that the sum of the all columns in the row is superior to 0您可以检查该行中所有列的总和是否优于0
df.output = df.apply(lambda row: int(row.sum() > 0), axis=1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.