[英]Create a Single column using values fom multiple columns
I am trying to create a new column in a Pandas data frame based on values from three columns,if the value for each column ['A','B','C'] is greater than 5 then output = 1 and output =0 if there is any value in either one of the columns ['A','B','C'] that is less then 5 The data frame looks like this: 我试图根据来自三列的值在Pandas数据框中创建一个新列,如果每列的值['A','B','C']大于5,则输出= 1,输出=如果列['A','B','C']中的任何一个值小于0,则返回0。5数据帧如下所示:
A B C
5 8 6
9 2 1
6 0 0
2 2 6
0 1 2
5 8 10
5 5 1
9 5 6
Expected output: 预期产量:
A B C new_column
5 8 6 1
9 2 1 0
6 0 0 0
2 2 6 0
0 1 2 0
5 8 10 1
5 5 1 0
9 5 6 1
I tried using this code,but it is not giving me the desired output: 我尝试使用此代码,但未提供所需的输出:
conditions = [(df['A'] >= 5) , (df['B'] >= 5) , (df['C'] >= 5)]
choices = [1,1,1]
df['new_colum'] = np.select(conditions, choices, default=0)
You need chain conditions by &
for bitwise AND
: 您需要按
&
进行bitwise AND
链条件:
conditions = (df['A'] >= 5) & (df['B'] >= 5) & (df['C'] >= 5)
Or use DataFrame.all
for check if all values in row are True
s: 或使用
DataFrame.all
检查行中的所有值是否均为True
:
conditions = (df[['A','B','C']] >= 5 ).all(axis=1)
#if need all columns >=5
conditions = (df >= 5 ).all(axis=1)
And then convert mask to integer for True, False
to 1, 0
: 然后将mask转换为
True, False
到1, 0
整数:
df['new_colum'] = conditions.astype(int)
Or use numpy.where
: 或使用
numpy.where
:
df['new_colum'] = np.where(conditions, 1, 0)
print (df)
A B C new_colum
0 5 8 6 1
1 9 2 1 0
2 6 0 0 0
3 2 2 6 0
4 0 1 2 0
5 5 8 10 1
6 5 5 1 0
7 9 5 6 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.