简体   繁体   中英

Python Map reduce

I am trying to have this code work in the Python anaconda spyder. I have to use Map -Reduce for the code

I have the csv file

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

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

# -*- 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('$',''))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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