Simon
Simon

Reputation: 312

Adding new column to dataframe of different length from list

I'm trying to add a new column to my dataframe for each time I run my funciton. This causes the error: 'ValueError: Length of values does not match length of index'. I assume this is because the list I add to df as a new column varies in length with every run of the function.

I have seen many threads suggest using concad, but this probably won't work for me as I can't seem to use concad and just overwrite my existing df - and I need one complete df at the end with a column from each run of my function.

My code functions like this:

df = DataFrame()
mylist = []

def myfunc(number):
    mylist = []
    for x in range(0,10):
        if 'some condition':
            mylist.append(x)
    df['results%d' % number] = mylist

So for each function iteration I'm adding contents of 'mylist' as a new dataframe column. At second iteration this causes above mentioned error. I need some way of letting python ignore index/column length. From the threads suggesting using concad, I get that passing that giving the instruction 'axis=1' fixes the problem of different lengths - so solution might be parallel to that.

Alternative I could create a range of lists either before function definition or at beginning of it - one list for each 'number' parameter passed to the function, but this is a very primitive solution

Upvotes: 0

Views: 3125

Answers (1)

Jason Bandlow
Jason Bandlow

Reputation: 93

I'm not exactly clear on what you're trying to do, but maybe you want something like this?

df = DataFrame()

def myfunc(number):
    row_index = 0
    for x in range(0,10):
        if 'some condition':
            df.loc[row_index, 'results%d' % number] = x
            row_index += 1

Upvotes: 1

Related Questions