There is a statistical and econometric library (statsmodels) that seems to handle this. Here is an example that extends @MSeifert, answering here on a similar question.
df=pd.DataFrame({ 'x':range(1,101), 'wt':range(1,101) }) from statsmodels.stats.weightstats import DescrStatsW wdf = DescrStatsW(df.x, weights=df.wt, ddof=1) print( wdf.mean ) print( wdf.std ) print( wdf.quantile([0.25,0.50,0.75]) )
67.0 23.6877840059 p 0.25 50 0.50 71 0.75 87
I do not use SAS, but this gives the same answer as the stata command:
sum x [fw=wt], detail
Actually, Stata has several weight options, in which case it gives a slightly different answer if you specify aw (analytical weights) instead of fw (frequency weights). In addition, stata requires fw be an integer, while DescrStatsW allows non-integer weights. Scales are harder than you think ... This starts to get into the weeds, but there is a lot of discussion about weight issues to calculate the standard deviation.
Also note that DescrStatsW does not include functions for min and max, but as long as your weights are not zero, this should not be a problem, since weights do not affect min and max. However, if you have zero weights, it might be nice to have weighted minimum and maximum values, but they are also easy to calculate in pandas:
df.x[ df.wt > 0 ].min() df.x[ df.wt > 0 ].max()
source share