用相应的元素解包两列列表

2024-04-28 07:09:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下数据框:

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]})

我试着去支撑和旋转,但没有成功,任何帮助都将不胜感激。你知道吗


Tags: 数据dataframedf列表onhronepd
1条回答
网友
1楼 · 发布于 2024-04-28 07:09:55

您可以使用^{}

#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

相关问题 更多 >