简体   繁体   English

Python Map减少

[英]Python Map reduce

I am trying to have this code work in the Python anaconda spyder. 我正在尝试在Python anaconda spyder中使用此代码。 I have to use Map -Reduce for the code 我必须使用Map -Reduce作为代码

I have the csv file 我有csv文件

I am trying to get rid of $ in payment amount so I could convert it to the float 我试图摆脱付款金额中的$ ,以便将其转换为浮动金额

Also I need to plot a graph for it 我也需要画一个图

so 所以

PayeeVendorName PayeAmount and Average payment made PayeeVendorName PayeAmountAverage付款

I have tried the following code but don't know how to work with the $ sign and convert to float from the string so i could parse it into map reduce 我已经尝试了以下代码,但不知道如何使用$符号并将其转换为从字符串浮点数,因此我可以将其解析为map reduce

# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""
import pandas as pd

import matplotlib.pyplot as plt

from mrjob.job import MRJob

class MRResearch(MRJob):

    def removeDollar(self, PaymentAmount):
        amount = PaymentAmount.str.replace ("$").as("").astype(float)
        return amount


    def mapper(self, key, line):

        ( CheckNumber, CheckDate, PayeeNumber, PayeeVendorName, PaymentAmount, ServiceTypeId, +
         ServiceTypeDesc, PurchaseOrder, PDMCU, DepartmentNbr, DepartmentName, BusinessUnitCode, +
         BusinessUnitName, VendorName, VendorAddress, VendorAddress_2, City, StateID, ZipCode) = line.split(',')

        PaymentMade = self.removeDollar(PaymentAmount)

        yield PayeeVendorName, float(PaymentMade)

    def reducer(self, PayeeVendorName, PaymentMade):
        total = 0
        numElement = 0
        for x in PaymentMade:
            total += x
            numElement += 1
        yield PayeeVendorName, total / numElement
if __name__ == '__main__':
    MRResearch.run()

    #   remove_dollar_sign = df["Amount"] = df["PAYMENT AMOUNT"].apply(lambda title: title).str.replace("$", "").str.replace(",","").astype(float

如果PaymentAmount是一个字符串,则将其转换为float将如下所示

 amount = float(PaymentAmount.replace('$',''))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM