Python Map减少

时间:2019-05-11 02:33:03

标签: python dictionary reduce

我正在尝试使此代码在Python anaconda spyder中工作。我必须对代码使用Map -Reduce

我有csv文件

我正试图摆脱$的付款金额,以便将其转换为 浮动

我还需要为其绘制图表

如此

PayeeVendorName PayeAmountAverage付款

我尝试了以下代码,但不知道如何使用 $签名并从字符串转换为float,以便我可以将其解析为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

1 个答案:

答案 0 :(得分:0)

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

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