首页 > 解决方案 > 如何从具有不同长度和条件的列中形成熊猫数据框?

问题描述

我有两列来自不同长度的不同数据帧(60,14),我想将 60 的每个项目与 14 的所有项目进行比较,然后将结果放在具有相关比较列的另一列中。我在列表列表中进行比较的结果是[outer_list for col_60[inner_list for result 1,1], [inner_list for result 1,2],....[inner_list for result(60,14]] 我的问题是如何以这种格式形成数据框?(col_60 = 60 行,col_14 = 14*60,col_result = 14*60 行):注意:列的项目是列表

col_60     col_14        col_result
              1          result_of(1,1)
              2          result_of(1,2)
              3             ..
  1           4
              ..
              ..            ..
              ..            ..
              13        result_of(1,13)
              14        result_of(1,14)
____________________________________________
             1          result_of(2,1)
             2          result_of(2,2)
             3             ..
  2          4
             ..
             ..            ..
             ..            ..
             13        result_of(2,13)
             14        result_of(2,14)
____________________________________________
            1          result_of(3,1)
            2          result_of(3,2)
            3             ..
  3         4
            ..
            ..            ..
            ..            ..
            13        result_of(3,13)
            14        result_of(3,14)
____________________________________________
              ..
              ..
              ..

我在这个问题中使用了接受的答案,但它堆叠了没有相关列的结果列,并且这个问题导致 NaN

标签: pythonpandasdataframe

解决方案


您可以使用分层索引解决此问题。这是一个示例,说明它如何处理前两对长度为 14 的组合。

import pandas as pd 



results = ["result(1,1)", "result(1,2)", "result(1,3)", ... "result(2,14)",] 
#put all the results in just one list instead of a list of lists

data = pd.Series(results, index = [['1', '1', '1', '1', '1', '1', #14 ones
                                  '1', '1', '1','1', '1', '1', '1', '1',
'2', '2', '2', '2', '2', '2', '2','2', '2', '2', '2', '2', '2', '2'], #14 two's
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 
 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]])

print(data)



1  1      result(1,1)
   2      result(1,2)
   3      result(1,3)
   4      result(1,4)
   5      result(1,5)
   6      result(1,6)
   7      result(1,7)
   8      result(1,8)
   9      result(1,9)
   10    result(1,10)
   11    result(1,11)
   12    result(1,12)
   13    result(1,13)
   14    result(1,14)
2  1      result(2,1)
   2      result(2,2)
   3      result(2,3)
   4      result(2,4)
   5      result(2,5)
   6      result(2,6)
   7      result(2,7)
   8      result(2,8)
   9      result(2,9)
   10    result(2,10)
   11    result(2,11)
   12    result(2,12)
   13    result(2,13)
   14    result(2,14)
dtype: object
>>> 

如果您将所有 60*14 的结果放在一个列表中,以下是准备好其他索引的代码:

first_index_raw = [[str(i)]*14 for i in range(1,60)]
first_index_final = [e for e in first_index_raw for e in e]
#a massive list that looks like this: [1,1,...1, 2, 2,....2, 3, 3,    
# 59, 59,...,60,60,...60] every element is repeated 14 times 

second_index = [i for i in range(1,15)]*60
#[1, 2,...14, 1, 2,...14,...1,2,...14] 60 times. 

data = pd.Series(results, index= [first_index_final,second_index])

确实,您得到的是系列而不是数据框,但我希望它有所帮助!


推荐阅读