Manipulations based on DataFrame column

I am using Python. I have the following code:

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"])

Resulting in the following dataframe:

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

I want to group the dataframe by Rating and in the meantime create additional columns showing the count and percentage of Function(Agent, Seller, Director) within each Rating. Expected result if as following:

  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%

Many thanks for the help. Cheers.

Upvotes: 1

Views: 26

Answers (1)

jezrael
jezrael

Reputation: 862511

Use crosstab first, then divide sum to new DataFrame, multiple by 100 and add_suffix for prevent duplicated columns names, last join together:

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

If want strings with %:

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%

Upvotes: 2

Related Questions