熊猫长到宽,根据答案填充值

时间:2018-03-26 01:46:55

标签: python pandas dataframe

我正在尝试将数据帧从long转换为宽,但我不知道如何将其转换为下面的格式。我错过了什么?

d = {'vote': [100, 50,1,23,55,67,89,44], 
     'vote2': [10, 2,18,26,77,99,9,40], 
     'ballot1': ['a','b','a','a','b','a','a','b'],
    'voteId':[1,2,3,4,5,6,7,8]}
df1=pd.DataFrame(d)
#########################################################

dftemp=df1

#####FORMATTING DATA
dftemp=pd.DataFrame(dftemp.reset_index())
dflw= dftemp.set_index(['voteId','vote','ballot1'])
dflw=dflw.unstack()
dflw.columns = dflw.columns.droplevel(0).rename('')
dflw=pd.DataFrame(dflw)
print(dflw)

我当前的输出:

               a    b     a     b
voteId vote                      
1      100   0.0  NaN  10.0   NaN
2      50    NaN  1.0   NaN   2.0

目标:

voteid (ballot1=a)vote (ballot1=b)vote (ballot1=a)vote2 (ballot1=b)vote2
1               100             NaN             10               NaN
2               NaN             50              NaN              2

1 个答案:

答案 0 :(得分:0)

我从df1开始

s=df1.set_index(['voteId','ballot1']).unstack()
s.columns=s.columns.map('(ballot1={0[1]}){0[0]}'.format) 

s
Out[1120]: 
        (ballot1=a)vote  (ballot1=b)vote  (ballot1=a)vote2  (ballot1=b)vote2
voteId                                                                      
1                 100.0              NaN              10.0               NaN
2                   NaN             50.0               NaN               2.0
3                   1.0              NaN              18.0               NaN
4                  23.0              NaN              26.0               NaN
5                   NaN             55.0               NaN              77.0
6                  67.0              NaN              99.0               NaN
7                  89.0              NaN               9.0               NaN
8                   NaN             44.0               NaN              40.0