简体   繁体   English

不同大小的列表到数据框

[英]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;例如,如果用户输入 5,10; then I have 2 lists.那么我有 2 个列表。 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然后我尝试将列表添加到数据框中,但由于列表的大小不同而无法添加并收到以下错误: 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; ie 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:这是我尝试将列表插入数据帧的代码(我是分钟列表中的哪个元素;即对于 5,10,我有 i=0 表示 5 和 i=1 表示 10 在循环中完成) list1 是一个列表,list2 是一个数据框:

 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)这行代码出现错误: 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.这是一个例子: List 1=[0.5,3.5,7.5] 然后我想将它插入到数据框中: list2.insert(i,i, list1)然后我想清空列表。 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)一旦它通过该函数,下一个列表将是 List1 is now= [7, 0.5, 8, 51.5, 2] 并且我想将其插入第 2 列,为什么我写了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=[]

IIUC, you want to set some list as a new dataframe column. IIUC,您想将一些列表设置为新的数据框列。

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. ValueError: Length of values does not match length of index很可能会出现,因为您试图将不同长度的列表插入到数据帧中。 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你可以使用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:您还可以使用DataFrame.fillna用您想要的任何内容填充这些DataFrame.fillna

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM