我正在使用Python。我有以下代码:
df=pd.DataFrame({"Function":["Agent","Seller","Agent","Director","Agent","Seller","Seller","Seller"],
"Rating":[1,2,1,3,7,7,3,1]}, index["John","Mathew","Martin","Clain","McGregor","Clause","Bob","Viktor"])
导致以下数据帧:
Name Function Rating
John Agent 1
Mathew Seller 2
Martin Agent 1
Clain Director 3
McGregor Agent 7
Clause Seller 7
Bob Seller 3
Viktor Seller 1
我想按评级对数据帧进行分组,同时创建额外的列,显示每个评级中功能(代理、卖方、主管)的数量和百分比。预期结果如下:
Rating Agents Seller Director Agent Seller Director
1 2 0 0 100% 0% 0%
2 0 1 0 0% 100% 0%
3 0 1 1 0% 50% 50%
7 1 1 0 50% 50% 0%
非常感谢你的帮助。 干杯
首先使用^{} ,然后将} 以防止列名称重复,最后一个^{} :
sum
除以新的DataFrame
,再乘以100
和^{如果需要带有
%
的字符串:相关问题 更多 >
编程相关推荐