![](/img/trans.png)
[英]Create multiple columns based on values in single column in Pandas DataFrame
[英]Create a Single column using values fom multiple columns
我试图根据来自三列的值在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
预期产量:
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
我尝试使用此代码,但未提供所需的输出:
conditions = [(df['A'] >= 5) , (df['B'] >= 5) , (df['C'] >= 5)]
choices = [1,1,1]
df['new_colum'] = np.select(conditions, choices, default=0)
您需要按&
进行bitwise AND
链条件:
conditions = (df['A'] >= 5) & (df['B'] >= 5) & (df['C'] >= 5)
或使用DataFrame.all
检查行中的所有值是否均为True
:
conditions = (df[['A','B','C']] >= 5 ).all(axis=1)
#if need all columns >=5
conditions = (df >= 5 ).all(axis=1)
然后将mask转换为True, False
到1, 0
整数:
df['new_colum'] = conditions.astype(int)
或使用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.