{{< localstorage language language-py >}}
{{< button-pydoc path=“apache_beam.transforms.combiners” class=“Mean” >}}
Transforms for computing the arithmetic mean of the elements in a collection, or the mean of the values associated with each key in a collection of key-value pairs.
In the following example, we create a pipeline with a PCollection
. Then, we get the element with the average value in different ways.
We use Mean.Globally()
to get the average of the elements from the entire PCollection
.
{{< highlight py >}} {{< code_sample “sdks/python/apache_beam/examples/snippets/transforms/aggregation/mean.py” mean_globally >}} {{< /highlight >}}
{{< paragraph class=“notebook-skip” >}} Output: {{< /paragraph >}}
{{< highlight class=“notebook-skip” >}} {{< code_sample “sdks/python/apache_beam/examples/snippets/transforms/aggregation/mean_test.py” mean_element >}} {{< /highlight >}}
{{< buttons-code-snippet py=“sdks/python/apache_beam/examples/snippets/transforms/aggregation/mean.py” >}}
We use Mean.PerKey()
to get the average of the elements for each unique key in a PCollection
of key-values.
{{< highlight py >}} {{< code_sample “sdks/python/apache_beam/examples/snippets/transforms/aggregation/mean.py” mean_per_key >}} {{< /highlight >}}
{{< paragraph class=“notebook-skip” >}} Output: {{< /paragraph >}}
{{< highlight class=“notebook-skip” >}} {{< code_sample “sdks/python/apache_beam/examples/snippets/transforms/aggregation/mean_test.py” elements_with_mean_value_per_key >}} {{< /highlight >}}
{{< buttons-code-snippet py=“sdks/python/apache_beam/examples/snippets/transforms/aggregation/mean.py” >}}
{{< button-pydoc path=“apache_beam.transforms.combiners” class=“Mean” >}}