Issue
Really struggling to get this solution. Suppose I have the dataframe below:
I would like to find the average number of occurrences of each ITEM based on the SEX group
| SEX | ITEM | Average |
|---|---|---|
| M | Socks | 0.6666 |
| M | Hat | 0.3333 |
| F | Socks | 0.25 |
| F | Hat | 0.75 |
Can anyone help me with this?
Solution
Let us try crosstab
out = pd.crosstab(df['SEX'], df['ITEM'],normalize='index').stack().reset_index(name='average')
Out[36]:
SEX ITEM average
0 F Hat 0.750000
1 F Socks 0.250000
2 M Hat 0.333333
3 M Socks 0.666667
Answered By - BENY Answer Checked By - David Marino (sql.easybugfix Volunteer)
Post a Comment
Post a Comment