[英]Creating a New DataFrame Column by Using a Comparison Operator
I have a DataFrame that looks like something similar to this: 我有一个看起来像这样的DataFrame:
0
0 3
1 11
2 7
3 15
And I want to add a column using two comparison operators. 我想使用两个比较运算符添加一列。 Something like this: 像这样:
df[1] = np.where(df[1]<= 10,1 & df[1]>10,0)
I want my return to look like this: 我希望我的返回看起来像这样:
0 1
0 3 1
1 11 0
2 7 1
3 15 0
But, I get this error message: 但是,我收到此错误消息:
TypeError: cannot compare a dtyped [float64] array with a scalar of type [bool]
Any help would be appreciated! 任何帮助,将不胜感激!
Setup 设定
df = pd.DataFrame({'0': {0: 3, 1: 11, 2: 7, 3: 15}})
Out[1292]:
0
0 3
1 11
2 7
3 15
Solution 解
#compare df['0'] to 10 and convert the results to int and assign it to df['1']
df['1'] = (df['0']<10).astype(int)
df
Out[1287]:
0 1
0 3 1
1 11 0
2 7 1
3 15 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.