How does one populate a row in pandas python
<p>I have initialised a dataframe in python to simulate a matrix</p>

<pre><code> llist = ["this", "is", "a","sentence"]
 df = pd.DataFrame(columns = llist, index = llist)
</code></pre>

<p>Now I want to populate a row as follows </p>

<pre><code> test = [1,0,0,0]
 df.iloc[[1]] = test
</code></pre>

<p>this throws up the following error</p>

<blockquote>
  <p>ValueError: cannot set using a list-like indexer with a different       length than the value</p>
</blockquote>

<p>When I set the value as below it populates the row with the same value </p>

<pre><code>  test = [1]
  df.iloc[[1]] = test
</code></pre>

<p>Can someone tell me why this happens and the most efficient way of populating a row with a list of different values? </p>

<p>I think you need remove one <code>[]</code>:</p>

<pre><code>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
</code></pre>

python list pandas dataframe append