融化pandas数据帧并根据列划分两行

时间:2017-04-05 12:51:36

标签: pandas melt

我有一个表单的dataFrame:

                   0
college     gender  
Engineering F      117
            M      2240

我希望将其更改为:

college       M:F
Engineering   19:1

其中M:F代表男性与女性的比例。

无论如何使用pandas执行此操作而不迭代所有行?

1 个答案:

答案 0 :(得分:1)

似乎你需要通过unstack重新塑造然后除以:

df = df.unstack()[0]
#same as
#df = df[0].unstack()
df['M:F'] = df['M'] / df['F']
#same as
#df['M:F']  = df['M'].div(df['F'])
print (df)
                        0
college      gender      
Engineering  F        117
             M       2240
Engineering1 F        117
             M       2240
df = df.unstack()[0]
df['M:F']  = df['M'].div(df['F'])
print (df)
gender          F     M        M:F
college                           
Engineering   117  2240  19.145299
Engineering1  117  2240  19.145299

print (df[['M:F']])
gender              M:F
college                
Engineering   19.145299
Engineering1  19.145299