Q:

finding average number of time a tuple value occurs using rdd

I have an rdd as below,
myrdd = sc.parallelize([("A", 2), ("B", 10), ("C", 4), ("A", 8), ("B", 6), ("B", 8), ("C", 10)])

I want to find out average number of for A, B, C occurs. I mean, I expect the result as follow,
('A', 5) # 2+8/2 = 5
('B', 8) # 10+6+8/3 = 8
('C', 7) # 4+10/2 = 7

If i use reduceByKey, i get only the total,
newrdd = myrdd.reduceByKey(lambda x, y: x + y)
list_ = newrdd.collect()

how do i do this only rdd operations?

A:

You can try the following:
collector_tuple = (0,0)

myrdd_avg = (
    myrdd.map(lambda row: (row[0], int(row[1])))
    .aggregateByKey(collector_tuple, 
                    lambda a,b: (a[0] + b,    a[1] + 1),
                    lambda a,b: (a[0] + b[0], a[1] + b[1]))
).map(lambda x: (x[0], x[1][0]/x[1][1]))
myrdd_avg.collect()
# [('A', 5.0), ('B', 8.0), ('C', 7.0)]

Straight reference: Calculating the averages for each KEY in a Pairwise (K,V) RDD in Spark with Python

