以下是您在杂货店中可以找到的食品数据列表。下方的CSV文件表示城市,食物类型,每磅平均价格以及加利福尼亚某城市的食物进餐时间。我需要确定在python中使用Map reduce函数,哪种食物类型的总价每磅最低。
Los Angeles,Vegetables,25.51,Breakfast
San Francisco,Fruits,259.32,Breakfast
Sandiego,Meat,22.94,Lunch
Sacramento,Dairy,53.71,Dinner
San Jose,Fish,44.16,Snack
Fresno,Poultry,393.05,Brunch
Oakland,Vegetables,15.99,Dinner
Bakersfield,Nuts,201.46,Dinner
Long Beach,Poultry,74.6,Snack
Anaheim,Grains,89.6,Breakfast
Riverside,Meat,152.75,Brunch
Irvine,Poultry,88.99,Brunch
Santa Barbara,Dairy,241.26,Lunch
Pasadena,Beans,789.7,Snack
这是我到目前为止所拥有的。任何帮助将不胜感激。
from mrjob.job import MRJob
class LowestPrice(MRJob):
def mapper(self, _, line):
line_cols = line.split(',')
yield line_cols[1], 1
if __name__ == '__main__':
LowestPrice.run()
import pandas as pd
data = pd.read_csv('/Users/shrek/Documents/data.csv', header=None)
print(data[2].min())
index = data[data[2]== data[2].min()].index[0]
print(data[1][index]+"in "+data[0][index]+" used in "+data[3][index]+" has lowest price per pound = "+str(data[2].min()))
输出将是:
Vegetablesin Oakland used in Dinner has lowest price per pound = 15.99