Converting 3d list into pandas single dataframe on same index

柔情痞子 提交于 2021-01-05 12:45:04

问题


My list l has shape np.array(l).shape (100,15,1) It has 100 dataframes with each df having 15 rows and 1 column. The index are same, just the sorting is different in each df of list. I want to unzip the list l:

l[0] =             Rank                  l[31] =           Rank
           A1       1                                A5      1
           A2       2                                A1      2
           A3       3                                A8      3
           A4       4.. till 15                      A3      4 .... also till 15

I want a single dataframe from this 3-d list l something like this: df= (15,100)

     0       1      2
A1   1       2      3
A2   2       3      2 
A3   3       6      1
A4   4       4      4
A5   5       8      6 .. till 100 columns and for all 15 indices

Basically, a dataframe with all ranking lists to be shown on same index only.


回答1:


Use concat with axis=1 and ignore_index=True for default new columns by range:

df = pd.concat(l, axis=1, ignore_index=True)
print (df)
      0    1
A1  1.0  2.0
A2  2.0  NaN
A3  3.0  4.0
A4  4.0  NaN
A5  NaN  1.0
A8  NaN  3.0


来源:https://stackoverflow.com/questions/64782474/converting-3d-list-into-pandas-single-dataframe-on-same-index

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!