如何使用pandas取消数据框的透视

时间:2018-04-22 02:45:06

标签: python pandas dataframe melt

例如,我有一个像这样的数据框

df = {'name':['Jennifer','Vivian','Trisha'],
     'married':[1,1,0],
     'Mon': [0, 0,1],
     'Tu':[1,0,0],
     'Wed':[0,1,0]}

enter image description here

如何将虚拟变量融合到一列中,如下所示:

enter image description here

我尝试使用pd.melt()但它只是将几列堆叠成一列并更改列的长度。有人可以帮我吗?提前谢谢!

2 个答案:

答案 0 :(得分:2)

这应该有效:

df.replace({'married':{1:'Married', 0: 'Single'}}). \
   melt(id_vars=['married', 'name'], var_name='Workday'). \
   query('value == 1'). \
   drop('value', axis=1)

#    married      name Workday
# 2   Single    Trisha     Mon
# 3  Married  Jennifer      Tu
# 7  Married    Vivian     Wed

答案 1 :(得分:0)

这是一种方式。

res = pd.melt(df, id_vars=['married', 'name'], value_vars=['Mon', 'Tu', 'Wed'],
              var_name='Workday')

res = res[res['value'] == 1].reset_index(drop=True)

d = {0: 'single', 1: 'married'}
res['married'] = res['married'].map(d)

print(res)

#    married      name Workday  value
# 0   single    Trisha     Mon      1
# 1  married  Jennifer      Tu      1
# 2  married    Vivian     Wed      1