小编fre*_*rog的帖子

根据阈值将NumPy数组转换为二进制向量

我有一个数组如下:

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)

我怎样才能做到这一点

python arrays numpy

10
推荐指数
1
解决办法
1万
查看次数

如何在python中删除错误类型的行?

我有一个这样的数据框:

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)

python type-conversion dataframe pandas

3
推荐指数
1
解决办法
669
查看次数

标签 统计

python ×2

arrays ×1

dataframe ×1

numpy ×1

pandas ×1

type-conversion ×1