Search code examples
pythonpandasinsertdataframecol

pandas python: adding blank columns to df


I'm trying to add x number of blank columns to a dataframe.

Here is my function:

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

but I get this error

ValueError: Wrong number of items passed 2, placement implies 1

I'm not sure what I'm doing wrong?


Solution

  • Try this:

    def fill_with_bars(old_df, number=10):
        empty_col = [' '*i for i in range(1,number+1)]
        tmp = df(columns=empty_col)
        return pd.concat([tmp,old_df], axis=1).fillna('')