TypeError:将2个字段相乘时,“ str”对象无法解释为整数

时间:2019-04-15 15:32:11

标签: python-3.x

我在文件中使用2列,即收入和保证金来计算每一行的利润,然后将其上传到SearchAds。我正在计算该函数的利润,并且抛出以下错误:

Traceback (most recent call last):
 File "C:\Python36\lib\site-packages\pandas\core\indexes\base.py", line 
 4381, in get_value
 return libindex.get_value_box(s, key)
 File "pandas\_libs\index.pyx", line 52, in pandas._libs.index.get_value_box
 File "pandas\_libs\index.pyx", line 48, in 
 pandas._libs.index.get_value_at
 File "pandas\_libs\util.pxd", line 113, in pandas._libs.util.get_value_at
 File "pandas\_libs\util.pxd", line 98, in 
pandas._libs.util.validate_indexer
TypeError: 'str' object cannot be interpreted as an integer
KeyError: 'MarginData'

我尝试在If子句之后立即计算利润,它仍然给我同样的错误。下面是代码。

for filename in os.listdir('//AwsSQl/Share/ftpdata/'):
    file = '//AwsSQl/Share/ftpdata/' + filename
    if filename.startswith('Revenue_'):
        print(filename)
        file_name = filename
        logging.info("Uploading Conversions from " + filename)
        columns = ['Timestamp', 'OrderID',  'Revenue', 'MarginPct']
        data = pd.read_csv(file, delimiter='\t')

        data['Revenue'] = data['Revenue'].map(lambda x: '{:.2f}'.format(x))
        data['MarginPct'] = data['MarginPct'].map(lambda x: '{:.2f}'.format(x))

        pd.set_option('display.max_columns', 500)
        pd.set_option('display.width', 1000)

        dir = '//AwsSQl/Share/ftpdata/'

        print(data.head(data['Timestamp'].count()))
        print(data['Timestamp'].count())

        for index, row in data.iterrows():
            dt = parse(row['Timestamp'])
            millisecond = int(round(dt.timestamp() * 1000))

            if row['Orders'] > 0:
                profit_upload(service, row['GCLID'], str(row['OrderID']) + "Pro" + str(index), millisecond, row['Revenue'], row['MarginData'])

defprofit_upload(服务,gclid,orderId,mill,rev,mar):     “”“将转化数据上传到Adobe-收入。

    Args:
        service: An authorized Doubleclicksearch service. See Set Up Your Application.
        gclid, orderId, millisecond, revenue, row
    """
request = service.conversion().insert(
    body=
    {
        'conversion': [{
            'agencyId': agencyId,
            'advertiserId': advertiserId,
            'attributionModel': 'External Attribution Model',
            'clickId': gclid,
            'conversionId': orderId,
            'conversionTimestamp': mill,
            'segmentationType': 'FLOODLIGHT',
            'segmentationName': 'Adobe - DSG - Profit',
            'type': 'Transaction',
            'revenueMicros': (round(float(rev), 2) * round(float(mar), 2) * 1000000), #10 million revenueMicros is equivalent to $10 of revenue
            'countMillis': 0 * 1000,
            'currencyCode': 'USD',
        }]
    }
)

1 个答案:

答案 0 :(得分:3)

似乎您有str类型,而您需要int类型。

... row['MarginData']) # <-- I expect this is where the problem starts

它找不到row['MarginData']的索引,或者期望其他内容。