Issue
I have DataFrame in Python Pandas like below:
Data type:
GROUP - int
TARGET - int
GROUP TARGET 0-5 1 0-5 0 20-25 1 40-45 1 ... ...
And I need to make result of the following calculation: df[df["TARGET"]==1].shape[0]] / df.shape[0]
for each group.
So as a result I need something like below:
GROUP | result | percent
------|---------|---------
0-5 | 0.005 | 0.5%
5-10 | 0.0093 | 0.93%
10-15 | 0.042 | 4.2%
15-20 | ... |
20-25 | ... |
25-30 | ... |
30-35 | ... |
35-40 | ... |
40-45 | ... |
45-50 | ... |
50-55 | ... |
55-60 | ... |
60-65 | ... |
65-70 | ... |
70-75 | ... |
75-80 | ... |
80-85 | ... |
85-90 | ... |
90-95 | ... |
95-100| ... |
How can I do that in Python Pandas ?
Solution
If there is only 0,1
use aggregation mean
:
df1 = df.groupby("GROUP", as_index=False)["TARGET"].mean()
If possible another values:
df1 = df["TARGET"].eq(1).groupby(df["GROUP"]).mean().reset_index()
Answered By - jezrael
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.