将列表字典列表转换为Python

时间:2016-08-14 02:40:16

标签: python pandas dictionary

我有一个字典列表,我想将其转换为数据框表。我知道标题中的问题有点令人困惑,所以在字典上发帖很糟糕:

[{'MESSAGE': ['RECEIVED SUCCESSFULLY AWAITING STOCKING PROCESS'], 'BIN': ['8FHA9D83H 82HG7D9F'], 'INV': 'SSXR   98-20LM NM CORN CREAM'}, {'MESSAGE': ['RECEIVED SUCCESSFULLY AWAITING STOCKING PROCESS', '#2956- INVALID STOCK COUPON CODE (MISSING).', 'RECEIVED SUCCESSFULLY AWAITING STOCKING PROCESS'], 'BIN': ['HA8DHWO2H HAHD0138', '8SHDNAIDU 00AD0123', '938273548 0967HDBR'], 'INV': 'FPBC   *SOUP CANS LENTILS'}]

我尝试了许多Pandas方法,但由于每个键都有多个值,因此要解决的问题稍微困难一些。我试图调整pd.DataFrame的传统方法,但我无法弄明白。我需要df表在打印时显示此输出:

         BIN               INV CODE                          MESSAGE                                                   
8FHA9D83H82HG7D9F   SSXR-98-20LM NM CORN CREAM  RECEIVED SUCCESSFULLY AWAITING STOCKING PROCESS
HA8DHWO2HHAHD0138     FPBC-*SOUP CANS LENTILS    RECEIVED SUCCESSFULLY AWAITING STOCKING PROCESS          
8SHDNAIDU00AD0123     FPBC-*SOUP CANS LENTILS    #2956- INVALID STOCK COUPON CODE (MISSING).          
9382735480967HDBR     FPBC-*SOUP CANS LENTILS    RECEIVED SUCCESSFULLY AWAITING STOCKING PROCESS          

我是Python 2.7的新手,所以任何帮助都会受到赞赏:)

1 个答案:

答案 0 :(得分:2)

假设L是您的词典列表:

pd.concat(pd.DataFrame(l) for l in L)

Out: 
                  BIN                           INV  \
0  8FHA9D83H 82HG7D9F  SSXR   98-20LM NM CORN CREAM   
0  HA8DHWO2H HAHD0138     FPBC   *SOUP CANS LENTILS   
1  8SHDNAIDU 00AD0123     FPBC   *SOUP CANS LENTILS   
2  938273548 0967HDBR     FPBC   *SOUP CANS LENTILS   

                                           MESSAGE  
0  RECEIVED SUCCESSFULLY AWAITING STOCKING PROCESS  
0  RECEIVED SUCCESSFULLY AWAITING STOCKING PROCESS  
1      #2956- INVALID STOCK COUPON CODE (MISSING).  
2  RECEIVED SUCCESSFULLY AWAITING STOCKING PROCESS