Python - Dataframe转置和更改结构

时间:2017-04-03 20:24:32

标签: python pandas dataframe data-structures transpose

我想转置df1并将每行的值“展开”成几行。同时,我不想使用列名称标记数据,而是将旧列名称添加到名为“segment”的新列下的每一行中。

以下是我的例子:

df1 = pd.DataFrame(index=['index1', 'index2', 'index3'], 
              columns=["A", "B", "C"], 
              data=[[5,np.nan, "ok"], [7,8,"fine"], ["3rd",100,np.nan]])
df1

我希望结果看起来像第二个数据帧df2:

df2 = pd.DataFrame(index=["index1", "index1", "index1", "index2", "index2", "index2", "index3", "index3", "index3"], 
              columns=['segment', 'value'], 
              data=[["A",5], ["B",np.nan], ["C","ok"], ["A",7], ["B",8], ["C","fine"],["A","3rd"],["B",100],["C",np.nan]])
df2

1 个答案:

答案 0 :(得分:2)

您可以使用DataFrame.stack(dropna=False)方法:

In [102]: df1.stack(dropna=False) \
             .rename_axis(['index','segment']) \
             .reset_index(level=1, name='value')
Out[102]:
       segment value
index
index1       A     5
index1       B   NaN
index1       C    ok
index2       A     7
index2       B     8
index2       C  fine
index3       A   3rd
index3       B   100
index3       C   NaN