aboutsummaryrefslogtreecommitdiff
path: root/python/pyspark/statcounter.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/pyspark/statcounter.py')
-rw-r--r--python/pyspark/statcounter.py22
1 files changed, 22 insertions, 0 deletions
diff --git a/python/pyspark/statcounter.py b/python/pyspark/statcounter.py
index 0fee3b2096..03ea0b6d33 100644
--- a/python/pyspark/statcounter.py
+++ b/python/pyspark/statcounter.py
@@ -131,6 +131,28 @@ class StatCounter(object):
def sampleStdev(self):
return sqrt(self.sampleVariance())
+ def asDict(self, sample=False):
+ """Returns the :class:`StatCounter` members as a ``dict``.
+
+ >>> sc.parallelize([1., 2., 3., 4.]).stats().asDict()
+ {'count': 4L,
+ 'max': 4.0,
+ 'mean': 2.5,
+ 'min': 1.0,
+ 'stdev': 1.2909944487358056,
+ 'sum': 10.0,
+ 'variance': 1.6666666666666667}
+ """
+ return {
+ 'count': self.count(),
+ 'mean': self.mean(),
+ 'sum': self.sum(),
+ 'min': self.min(),
+ 'max': self.max(),
+ 'stdev': self.stdev() if sample else self.sampleStdev(),
+ 'variance': self.variance() if sample else self.sampleVariance()
+ }
+
def __repr__(self):
return ("(count: %s, mean: %s, stdev: %s, max: %s, min: %s)" %
(self.count(), self.mean(), self.stdev(), self.max(), self.min()))