我有一个数组如下:
a=np.array([0.1, 0.2, 0.3, 0.7, 0.8, 0.9])
Run Code Online (Sandbox Code Playgroud)
我想要的是根据阈值将此向量转换为二进制向量.以threshold = 0.5为例,大于0.5的元素转换为1,否则为0.
输出向量应如下所示:
a_output = [0, 0, 0, 1, 1, 1]
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点
我有一个这样的数据框:
import pandas as pd
test_df = pd.DataFrame({'foo':['1','2','92#']})
test_df
foo
0 1
1 2
2 92#
Run Code Online (Sandbox Code Playgroud)
我想将类型转换为int64:
test_df.foo.astype('int64')
Run Code Online (Sandbox Code Playgroud)
但我收到错误消息,因为'92#'无法转换为int64:
ValueError:基数为10的int()的无效文字:'92#'
所以我想删除所有无法转换为int64的行,并得到我的结果:
foo
0 1
1 2
Run Code Online (Sandbox Code Playgroud)