Skip to content

Commit 27cb54b

Browse files
committed
Added keys and values methods for PairFunctions in python
1 parent 4ce76b3 commit 27cb54b

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

python/pyspark/rdd.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,26 @@ def collectAsMap(self):
690690
"""
691691
return dict(self.collect())
692692

693+
def keys(self):
694+
"""
695+
Return an RDD with the keys of each tuple.
696+
>>> m = sc.parallelize([(1, 2), (3, 4)]).keys()
697+
>>> m.collect()
698+
[1, 3]
699+
"""
700+
map_only_keys_fn = lambda (k, v): k
701+
return self.map(map_only_keys_fn)
702+
703+
def values(self):
704+
"""
705+
Return an RDD with the values of each tuple.
706+
>>> m = sc.parallelize([(1, 2), (3, 4)]).values()
707+
>>> m.collect()
708+
[2, 4]
709+
"""
710+
map_only_values_fn = lambda (k, v): v
711+
return self.map(map_only_values_fn)
712+
693713
def reduceByKey(self, func, numPartitions=None):
694714
"""
695715
Merge the values for each key using an associative reduce function.

0 commit comments

Comments
 (0)