0

我正在创建一个 numpy 数组,然后将其导出到 django 模型中。作为 dtype 属性,我有一个 None,但我有一列应该是一个允许 NULL 值的整数。现在,当在 csv 中找到一个“NULL”字符串时,该列的类型在 bool 中发生了变化,它不接受 None。

现在,是否有可能dtype={'name of the columns', 'int'}只为该列做一些事情,让其余的仍然在 None 上,让 numpy 决定类型?

4

1 回答 1

0

假设您正在谈论 genfromtxt,您可以使用 dtype 参数设置 dtype:

例如

如果您的文件包含

1.0 2.0 3.0 4.0
1.0 2.0 3.0 4.0
1.0 2.0 3.0 4.0
1.0 2.0 3.0 hello

然后

a=np.genfromtxt('vlen.txt',dtype=[('col0', 'i4'), ('col1', '<f8'), ('col2', '<f8'), ('col3', 'S5')])

a['col0']
array([1, 1, 1, 1]) # note ints not floats
于 2014-03-31T15:03:59.280 回答