我已经在 python 中初始化了一个数据框来模拟一个矩阵
llist = ["this", "is", "a","sentence"]
df = pd.DataFrame(columns = llist, index = llist)
现在我想填充一行如下
test = [1,0,0,0]
df.iloc[[1]] = test
这会引发以下错误
ValueError: cannot set using a list-like indexer with a different length than the value
当我如下设置值时,它会用相同的值填充行
test = [1]
df.iloc[[1]] = test
有人能告诉我为什么会发生这种情况,以及用不同值的列表填充行的最有效方法吗?
请您参考如下方法:
我认为你需要删除一个[]:
test = [1,0,0,0]
df.iloc[1] = test
print (df)
this is a sentence
this NaN NaN NaN NaN
is 1 0 0 0
a NaN NaN NaN NaN
sentence NaN NaN NaN NaN
