我试图在一个数据帧中添加x个空列。在
我的职能是:
def fill_with_bars(df, number=10):
'''
Add blank, empty columns to dataframe, at position 0
'''
numofcols = len(df.columns)
while numofcols < number:
whitespace = ''
df.insert(0, whitespace, whitespace, allow_duplicates=True)
whitespace += whitespace
return df
但我得到了这个错误
^{pr2}$我不知道我做错了什么?在
试试这个:
我不想一次插入一列,而是创建一个所需维度的df,然后调用
concat
:至于你为什么会犯这个错误:
因为str不是一个空格,而是一个空字符串:
^{pr2}$因此,在第3次迭代中,它试图查找列,期望只有一个列,但是有2个列,因此内部检查失败,因为它现在找到了2个名为
''
的列。在相关问题 更多 >
编程相关推荐