基于DataFrame列的操作

2024-09-30 10:42:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用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%

非常感谢你的帮助。 干杯


Tags: 数据代码clausefunctionjohnagentbobmartin
1条回答
网友
1楼 · 发布于 2024-09-30 10:42:53

首先使用^{},然后将sum除以新的DataFrame,再乘以100^{}以防止列名称重复,最后一个^{}

df1 = pd.crosstab(df['Rating'], df['Function'])

df2 = df1.div(df1.sum(axis=1), 0).mul(100).add_suffix('%').round(2)

df = df1.join(df2).reset_index().rename_axis(None, axis=1)
print (df)
   Rating  Agent  Director  Seller  Agent%  Director%  Seller%
0       1      2         0       1   66.67        0.0    33.33
1       2      0         0       1    0.00        0.0   100.00
2       3      0         1       1    0.00       50.0    50.00
3       7      1         0       1   50.00        0.0    50.00

如果需要带有%的字符串:

df2 = df1.div(df1.sum(axis=1), 0).mul(100).add_suffix('%').round(2).astype(str).add('%')

df = df1.join(df2).reset_index().rename_axis(None, axis=1)
print (df)

   Rating  Agent  Director  Seller  Agent% Director% Seller%
0       1      2         0       1  66.67%      0.0%  33.33%
1       2      0         0       1    0.0%      0.0%  100.0%
2       3      0         1       1    0.0%     50.0%   50.0%
3       7      1         0       1   50.0%      0.0%   50.0%

相关问题 更多 >

    热门问题