使用字典条目拆分/分解Pandas列

我有一个这样的数据框。

index          column1
e1             {u'c680': 5, u'c681': 1, u'c682': 2...
e2             {u'c780': 6, u'c781': 2, u'c782': 1...
e3             {u'c880': 2, u'c881': 4, u'c882': 2...
e4             {u'c980': 4, u'c981': 2, u'c982': 3...

现在,我想将column1中的dict扩展到如下所示的各个列。

index   colname           c681
e1        c680              5
e1        c681              1
e1        c682              2
e2        c780              6
e2        c781              2
e2        c782              1

我经历了几个类似的建议使用的答案:

df.column1.apply(pd.Series)

但是,这以不同的方式爆炸了列...有没有办法使列显示得更好?

评论
  • 猜不透o
    猜不透o 回复

    You can flatten Series of dictionaries in list comprehension and pass to DataFrame constructor:

    df1 = pd.DataFrame([(k, ) + y for k, v in df.column1.items() for y in v.items()], 
                        columns=['idx','colname', 'c681'])
    print (df1)
       idx colname  c681
    0   e1    c680     5
    1   e1    c681     1
    2   e1    c682     2
    3   e2    c780     6
    4   e2    c781     2
    5   e2    c782     1
    6   e3    c880     2
    7   e3    c881     4
    8   e3    c882     2
    9   e4    c980     4
    10  e4    c981     2
    11  e4    c982     3
    
  • 脆弱不堪
    脆弱不堪 回复

    You could map the dictionaries with dict.items, flatten the resulting tuples with itertools.chain and reconstruct the dataframe:

    from itertools import chain
    
    chained = chain.from_iterable(df.column1.map(dict.items))
    pd.DataFrame(chained , columns=['colname', 'c681'])
    
      colname  c681
    0    c680     5
    1    c681     1
    2    c682     2
    3    c780     6
    4    c781     2
    5    c782     1