簡體   English   中英

如何將字符串值分配給 numpy 數組中的特定行和列?

[英]How to assign a string value to a particular row and column in numpy array?

我在 python 中創建了一個空指標說

metric = np.empty([1,6])

現在,我正在向其特定的行和列插入一些值,如下所示

metric[0:1,1:2] = 1.23
metric[0:1,2:3] = 2.4
metric[0:1,3:4] = 20
metric[0:1,4:5] = 10
metric[0:1,5:6] = 0.56
metric[0:1,6:7] = 0.50

現在,除了整數值,我想在第一行的第 6 列中插入一個字符串。 IE

metric[0:1,6:7] = str('5') + "Days"

我想在第一行的第 6 列中插入“5 天”。 這該怎么做?? 請建議...

這是因為 dtype,np 數組的默認 dtype 是 float。 您需要將其設置為對象。

import numpy as np

metric = np.empty([1, 7], dtype=object)

metric[0,1] = 1.23
metric[0,2] = 2.4
metric[0,3] = 20
metric[0,4] = 10
metric[0,5] = 0.56
metric[0,6] = 'Days 0.50'

print(metric)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM