解压缩两列具有相应元素的列表

时间:2016-07-20 19:17:22

标签: python python-2.7 pandas

我有以下数据框:

df = pd.DataFrame({'A' : [['on', 'ne', 'on'], ['tw'],
                         ['th', 'hr', 'ree'], []],
                   'B' : ['one', 'two', 'three','four'],
                   'C' : [0.2,0.6,-1.4,0.7],
                   'D' : [[0.2,0.3,-1.2],[0.5],
                         [0.9,0.1,0.0],[]]})

A和D是两列具有相应值的列表。 我只是想解压缩值,使它成为这个。

df = pd.DataFrame({'A' : ['on', 'ne', 'on', 'tw',
                         'th', 'hr', 'ree', N/A],
                   'B' : ['one', 'one','one','two',
                          'three', 'three','three','four'],
                   'C' : [0.2, 0.2, 0.2, 0.6,
                          -1.4, -1.4, -1.4, 0.7],
                   'D' : [0.2, 0.3, -1.2, 0.5,
                         0.9, 0.1, 0.0, N/A]})

我尝试了拆卸和枢轴但没有成功,任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

您可以使用join

#DataFrame from Series, remove level 1
df1 = pd.DataFrame({'A':df.A.apply(pd.Series).stack(), 
                    'D':df.D.apply(pd.Series).stack()}).reset_index(drop=True, level=1)
print (df1)   
     A    D
0  foo  0.2
0  bar  0.3
0  foo -1.2
1  bar  0.5
2  foo  0.9
2  bar  0.1
2  foo  0.0

#join new df1 to subset df(columns B,C) and sort columns
print (df[['B','C']].join(df1).sort_index(axis=1))
     A      B    C    D
0  foo    one  0.2  0.2
0  bar    one  0.2  0.3
0  foo    one  0.2 -1.2
1  bar    two  0.6  0.5
2  foo  three -1.4  0.9
2  bar  three -1.4  0.1
2  foo  three -1.4  0.0
3  NaN    two  0.7  NaN
#reset index
print (df[['B','C']].join(df1).sort_index(axis=1).reset_index(drop=True))
     A      B    C    D
0  foo    one  0.2  0.2
1  bar    one  0.2  0.3
2  foo    one  0.2 -1.2
3  bar    two  0.6  0.5
4  foo  three -1.4  0.9
5  bar  three -1.4  0.1
6  foo  three -1.4  0.0
7  NaN    two  0.7  NaN