Monica
Monica

Reputation: 5

Different sized lists to dataframe

I have the user input a list of minutes and then a specific size. I then have a function that calculates the average price difference for every time that size shows up and this value is added to a list. Thus, I will have as many lists as the length of minutes. For example, if the user inputs 5,10; then I have 2 lists. I then try to add the list into a data frame, but am unable to as the lists are different in size and get an error that : ValueError: Length of values does not match length of index

This is the code I have for trying to insert the list into the dataframe (i is which element in the minutes list; i.e for 5,10 I have i=0 for 5 and i=1 for 10 done in a loop) list1 is a list, list2 is a dataframe:

 list2.insert(i,i, list1)  
 export_csv = list2.to_csv(file2 ,index = None, header=False)
 list1=[]

The error comes for this line of code: list2.insert(i,i, list1)

Here is an example: List 1=[0.5,3.5,7.5] then I want to insert it into the data frame: list2.insert(i,i, list1) Then I want to empty the list. Once it goes through the function the next list will be List1 is now= [7, 0.5, 8, 51.5, 2] and I want to insert that into column 2, why I wrote list2.insert(i,i, list1)

Here is my full code if necessary:

#df is a data frame
#b is also a dataframe
#list2 is a dataframe
#list1 is a list

for i in range (0, len(df)):
            size=df.iloc[i,0]
            for i in range(0,len(numbers)-1):            
                for number in numbers:
                #for i in range (0, len(numbers)-1):
                    print(number)
                    for filename in filenames:
                        b['diff']=abs(b['price']-b['orig_price'])
                        list1.extend((b['diff']))
                        print('size', size, list1)                    
                    list2[i+size+number]=list1 
                    export_csv = list2.to_csv(file2 ,index = None, header=True)
                    list1=[]

Upvotes: 0

Views: 5538

Answers (1)

dkhara
dkhara

Reputation: 715

IIUC, you want to set some list as a new dataframe column.

The ValueError: Length of values does not match length of index is most likely coming up because you're trying to insert a list of different length to a dataframe. In other words, make sure the length of your list equals the the number of rows in your dataframe. Otherwise, you will keep receiving this error. If you want to see a slightly more efficient way of creating new columns, keep reading.

Let's start with a sample list:

print(l)                                                                                                                        
[1, 2, 3]

And a sample dataframe:

print(df)                                                                                                                       
  c1  c2  c3
0  a   8   6
1  b   8   6
2  c   8   6

Then you can simply assign the list to a new column by:

df['new_lst_variable'] = l 

print(df)                                                                                                                       
  c1  c2  c3  new_lst_variable
0  a   8   6                 1
1  b   8   6                 2
2  c   8   6                 3

Update

If you have a list that doesn't quite match up with the number of rows in your dataframe:

l2 = [1, 2, 3, 4]

You could use pandas.concat

df = pd.concat([df,pd.Series(l2)], ignore_index=True, axis=1)

print(df)                                                              
     0    1    2  3
0    a  8.0  6.0  1
1    b  8.0  6.0  2
2    c  8.0  6.0  3
3  NaN  NaN  NaN  4

You could also use DataFrame.fillna to fill these nans with whatever you'd like:

df = df.fillna(0)

print(df)                                                              
   0    1    2  3
0  a  8.0  6.0  1
1  b  8.0  6.0  2
2  c  8.0  6.0  3
3  0  0.0  0.0  4

Upvotes: 3

Related Questions