I have initialised a dataframe in python to simulate a matrix
llist = ["this", "is", "a","sentence"]
df = pd.DataFrame(columns = llist, index = llist)
Now I want to populate a row as follows
test = [1,0,0,0]
df.iloc[[1]] = test
this throws up the following error
ValueError: cannot set using a list-like indexer with a different length than the value
When I set the value as below it populates the row with the same value
test = [1]
df.iloc[[1]] = test
Can someone tell me why this happens and the most efficient way of populating a row with a list of different values?
I think you need remove one []
:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.