我想计算数据框中两个类别之间的差异。例如,在下面的例子中,我想计算男性和女性在每项工作中的差异。但是,有些工作只能由男性或女性完成。什么是有效的方法?谢谢。
import pandas as pd
pd.DataFrame({'job': ['a', 'a', 'b', 'b', 'c'], 'gender':['M', 'F', 'M', 'F', 'M'], 'income':[300, 200, 450, 400, 350]})
Out[3]:
gender income job
0 M 300 a
1 F 200 a
2 M 450 b
3 F 400 b
4 M 350 c
请您参考如下方法:
您可以做一个枢轴,使同一份工作的男性和女性薪酬在同一行。然后您可以直观地比较或运行其他基于行的代码。
import pandas as pd
df = pd.DataFrame({'job': ['a', 'a', 'b', 'b', 'c'], 'gender':['M', 'F', 'M', 'F', 'M'], 'income':[300, 200, 450, 400, 350]})
compare_income_by_gender_df = df.pivot(index='job', columns='gender', values='income')
print compare_income_by_gender_df
导致
python pivot.py
gender F M
job
a 200 300
b 400 450
c NaN 350
