使用python中的mrjob查找收入最高的前3个城市

7xllpg7q  于 2021-05-29  发布在  Hadoop
关注(0)|答案(0)|浏览(219)

我需要帮助输出前三大城市有最多的收入。现在我只看到所有城市的总收入都在输出,但我需要把输出限制在前三位。
我有所有城市的总收入输出。

"""
 Python script to find the total amount of sales revenue for each city
 using Map-Reduce framework (mapper, combiner, and reducer functions) with mrjob package
 4/14/17
"""
from mrjob.job import MRJob

class CityRevenue(MRJob):

# each input lines consists of city, productCategory, price, and paymentMode

    def mapper(self, _, line):
        # create a key-value pair with key: city and value: price
        line_cols = line.split(',')
        yield line_cols[0], float(line_cols[2])
    def combiner(self, city, counts):
        # consolidates all key-value pairs of mapper function (performed at mapper nodes)
        yield city, sum(counts)
    def reducer(self, city, counts):
        # final consolidation of key-value pairs at reducer nodes
        yield city, '${:,.2f}'.format(sum(counts))
if __name__ == '__main__':
    CityRevenue.run()

实际值:

"Albuquerque"   "$1,208,490.13"
"Anaheim"       "$1,264,165.71"
"Anchorage"     "$1,191,057.61"
"Arlington"     "$1,229,375.89"
"Atlanta"       "$1,216,153.47"
"Aurora"        "$1,216,807.43"
"Austin"        "$1,208,925.21"
"Bakersfield"   "$1,211,742.46"
"Baltimore"     "$1,225,227.14"
"Baton Rouge"   "$1,214,852.71"
"Birmingham"    "$1,218,785.75"
"Boise"         "$1,216,941.64"
"Boston"        "$1,204,833.39"
"Buffalo"       "$1,190,531.15"
"Chandler"      "$1,192,263.53"
"Charlotte"     "$1,233,641.50"
"Chesapeake"    "$1,242,760.99"
"Chicago"       "$1,219,848.29"
"Chula Vista"   "$1,241,528.46"
"Cincinnati"    "$1,218,642.84"

预期:

"Anaheim"       "$1,264,165.71"
"Chesapeake"    "$1,242,760.99"
"Chula Vista"   "$1,241,528.46"

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题