Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weighting results in pandas crosstab

I would like to use a third column to weight results in a pandas crosstab.

For example, the following:

import pandas as pd
df = pd.DataFrame({'A': ['foo', 'bar', 'foo', 'bar', 'bar'],
                   'B': [1, 1, 0, 0, 0],
                   'weight': [2, 3, 4, 5, 6]})
print(pd.crosstab(df.A, df.B))

results in:

B    0  1
A        
bar  2  1
foo  1  1

What I would like as a result is:

B     0  1
A        
bar  11  3
foo   4  2
like image 489
prooffreader Avatar asked Dec 04 '25 16:12

prooffreader


1 Answers

You can supply a custom aggregate function to a crosstab using the aggfunc parameter:

pd.crosstab(df.A, df.B, df.weight, aggfunc = sum)
B     0  1
A         
bar  11  3
foo   4  2
like image 182
maxymoo Avatar answered Dec 06 '25 18:12

maxymoo