Adding items to empty pandas DataFrame
<p>I want to dynamically extend an empty pandas DataFrame in the following way:</p>

<pre><code>df=pd.DataFrame()
indices=['A','B','C']
colums=['C1','C2','C3']
for colum in colums:
    for index in indices:
        #df[index,column] = anyValue
</code></pre>

<p>Where both indices and colums can have arbitrary sizes which are not known in advance, i.e. I cannot create a DataFrame with the correct size in advance.</p>

<p>Which pandas function can I use for </p>

<pre><code>#df[index,column] = anyValue
</code></pre>

<p>?</p>

<p>I think you can use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.loc.html" rel="nofollow"><code>loc</code></a>:</p>

<pre><code>df = pd.DataFrame()

df.loc[0,1] = 10
df.loc[2,8] = 100
print(df)
      1      8
0  10.0    NaN
2   NaN  100.0
</code></pre>

<p>Faster solution with <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.set_value.html" rel="nofollow"><code>DataFrame.set_value</code></a>:</p>

<pre><code>df = pd.DataFrame()
indices = ['A', 'B', 'C']
columns = ['C1', 'C2', 'C3']
for column in columns:
    for index in indices:
       df.set_value(index, column, 1)

print(df)
    C1   C2   C3
A  1.0  1.0  1.0
B  1.0  1.0  1.0
C  1.0  1.0  1.0
</code></pre>

python pandas