Python 使用比较运算符创建新的DataFrame列
我有一个数据框,看起来像这样:Python 使用比较运算符创建新的DataFrame列,python,pandas,dataframe,Python,Pandas,Dataframe,我有一个数据框,看起来像这样: 0 0 3 1 11 2 7 3 15 我想用两个比较运算符添加一列。大概是这样的: df[1] = np.where(df[1]<= 10,1 & df[1]>10,0) 但是,我收到以下错误消息: TypeError: cannot compare a dtyped [float64] array with a scalar of type [bool] 任何帮助都将不胜感激 设置 解决方案 10,1和10,0应该
0
0 3
1 11
2 7
3 15
我想用两个比较运算符添加一列。大概是这样的:
df[1] = np.where(df[1]<= 10,1 & df[1]>10,0)
但是,我收到以下错误消息:
TypeError: cannot compare a dtyped [float64] array with a scalar of type [bool]
任何帮助都将不胜感激 设置
解决方案
10,1和10,0应该改为10.1和10.0吗?因为事实上,它看起来像是在where中评估三件事情,其中一件是1&df[1]>10,这可能是导致类型错误的原因。太棒了。谢谢你的帮助。我真的很感激!别担心。如果有用,请接受它作为答案。
TypeError: cannot compare a dtyped [float64] array with a scalar of type [bool]
df = pd.DataFrame({'0': {0: 3, 1: 11, 2: 7, 3: 15}})
Out[1292]:
0
0 3
1 11
2 7
3 15
#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