擅长:python、mysql、java
<p>我不想一次插入一列,而是创建一个所需维度的df,然后调用<code>concat</code>:</p>
<pre><code>In [72]:
def fill_with_bars(df, number=10):
return pd.concat([pd.DataFrame([],index=df.index, columns=range(10)).fillna(''), df], axis=1)
df = pd.DataFrame({'a':np.arange(10), 'b':np.arange(10)})
fill_with_bars(df)
Out[72]:
0 1 2 3 4 5 6 7 8 9 a b
0 0 0
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
8 8 8
9 9 9
</code></pre>
<p>至于你为什么会犯这个错误:</p>
<p>因为str不是一个空格,而是一个空字符串:</p>
^{pr2}$
<p>因此,在第3次迭代中,它试图查找列,期望只有一个列,但是有2个列,因此内部检查失败,因为它现在找到了2个名为<code>''</code>的列。在</p>