Search code examples
python-3.xdata-analysis

TypeError: unsupported operand type(s) for /: 'float' and 'method' while trying to find the ratio of 2 columns in a data frame


I'm trying to find the ratio of 2 columns in a dataframe and store it in a new column in the same dataframe while doing that i'm facing the following error TypeError: unsupported operand type(s) for /: 'float' and 'method' .

My dataset has is nearly 8950 rows and 21 column and i have removed all the NAN's.

>> credit.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 8950 entries, 0 to 8949
Data columns (total 21 columns):
CUST_ID                             8950 non-null object
BALANCE                             8950 non-null float64
BALANCE_FREQUENCY                   8950 non-null float64
PURCHASES                           8950 non-null float64
ONEOFF_PURCHASES                    8950 non-null float64
INSTALLMENTS_PURCHASES              8950 non-null float64
CASH_ADVANCE                        8950 non-null float64
PURCHASES_FREQUENCY                 8950 non-null float64
ONEOFF_PURCHASES_FREQUENCY          8950 non-null float64
PURCHASES_INSTALLMENTS_FREQUENCY    8950 non-null float64
CASH_ADVANCE_FREQUENCY              8950 non-null float64
CASH_ADVANCE_TRX                    8950 non-null int64
PURCHASES_TRX                       8950 non-null int64
CREDIT_LIMIT                        8950 non-null object
PAYMENTS                            8950 non-null float64
MINIMUM_PAYMENTS                    8950 non-null object
PRC_FULL_PAYMENT                    8950 non-null float64
TENURE                              8950 non-null int64
Monthly_avg_purchase                8950 non-null float64
Monthly_cash_advance                8950 non-null float64
purchase_type                       8950 non-null object
dtypes: float64(14), int64(3), object(4)
memory usage: 1.3+ MB

and when i'm trying to find the ratio i'm hitting error. the error string and the code are below.

>> credit['LIMIT_USAGE'] = credit['BALANCE']/credit['CREDIT_LIMIT']

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\ops\__init__.py in na_op(x, y)
    967         try:
--> 968             result = expressions.evaluate(op, str_rep, x, y, **eval_kwargs)
    969         except TypeError:

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\computation\expressions.py in evaluate(op, op_str, a, b, use_numexpr, **eval_kwargs)
    220     if use_numexpr:
--> 221         return _evaluate(op, op_str, a, b, **eval_kwargs)
    222     return _evaluate_standard(op, op_str, a, b)

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\computation\expressions.py in _evaluate_numexpr(op, op_str, a, b, truediv, reversed, **eval_kwargs)
    126     if result is None:
--> 127         result = _evaluate_standard(op, op_str, a, b)
    128 

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\computation\expressions.py in _evaluate_standard(op, op_str, a, b, **eval_kwargs)
     69     with np.errstate(all="ignore"):
---> 70         return op(a, b)
     71 

TypeError: unsupported operand type(s) for /: 'float' and 'method'

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-40-b24cdb4eaf3e> in <module>
----> 1 credit['LIMIT_USAGE'] = credit['BALANCE']/credit['CREDIT_LIMIT']

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\ops\__init__.py in wrapper(left, right)
   1046 
   1047         with np.errstate(all="ignore"):
-> 1048             result = na_op(lvalues, rvalues)
   1049         return construct_result(
   1050             left, result, index=left.index, name=res_name, dtype=None

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\ops\__init__.py in na_op(x, y)
    968             result = expressions.evaluate(op, str_rep, x, y, **eval_kwargs)
    969         except TypeError:
--> 970             result = masked_arith_op(x, y, op)
    971 
    972         return missing.dispatch_fill_zeros(op, x, y, result)

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\ops\__init__.py in masked_arith_op(x, y, op)
    445         if mask.any():
    446             with np.errstate(all="ignore"):
--> 447                 result[mask] = op(xrav[mask], com.values_from_object(yrav[mask]))
    448 
    449     else:

TypeError: unsupported operand type(s) for /: 'float' and 'method'

I have tried using apply function also and even that did not work. the code and error for the apply is pasted below

>> credit['limit_usage']=credit.apply(lambda x: x['BALANCE']/x['CREDIT_LIMIT'], axis=1)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-43-2b06fadbf803> in <module>
----> 1 credit['limit_usage']=credit.apply(lambda x: x['BALANCE']/x['CREDIT_LIMIT'], axis=1)

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py in apply(self, func, axis, broadcast, raw, reduce, result_type, args, **kwds)
   6911             kwds=kwds,
   6912         )
-> 6913         return op.get_result()
   6914 
   6915     def applymap(self, func):

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\apply.py in get_result(self)
    184             return self.apply_raw()
    185 
--> 186         return self.apply_standard()
    187 
    188     def apply_empty_result(self):

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\apply.py in apply_standard(self)
    290 
    291         # compute the result using the series generator
--> 292         self.apply_series_generator()
    293 
    294         # wrap results

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\apply.py in apply_series_generator(self)
    319             try:
    320                 for i, v in enumerate(series_gen):
--> 321                     results[i] = self.f(v)
    322                     keys.append(v.name)
    323             except Exception as e:

<ipython-input-43-2b06fadbf803> in <lambda>(x)
----> 1 credit['limit_usage']=credit.apply(lambda x: x['BALANCE']/x['CREDIT_LIMIT'], axis=1)

TypeError: ("unsupported operand type(s) for /: 'float' and 'method'", 'occurred at index 5203')

Please suggest a quick solution for this.

Thanks in advance.


Solution

  • Just a slight modification to your code is needed to make it work.

    >>> df = pd.DataFrame({"BALANCE": [100, 255], "CREDIT_BALANCE": [10, 15]})
    >>> df
       BALANCE  CREDIT_BALANCE
    0      100              10
    1      255              15
    >>>
    >>> df["limit_usage"] = df.apply(lambda row: row.BALANCE / row.CREDIT_BALANCE, axis=1)
    >>> df
       BALANCE  CREDIT_BALANCE  limit_usage
    0      100              10         10.0
    1      255              15         17.0
    >>>
    

    Try something like that and see if that makes a difference.


    The Apply-function above works only if both values can be divided.

    If you have a custom class, then you can do something like this to make it work:

    class things:
        def __init__(self, var):
            self.var = var
        def __int__(self):
            return int(self.var)
        def __repr__(self):
            return "things({})".format(self.var)
    df = pd.DataFrame({"BALANCE": [100, 255], "CREDIT_BALANCE": [things(10), things(15)]})
    df["limit_usage"] = df.apply(lambda row: row.BALANCE / int(row.CREDIT_BALANCE), axis=1)
    print(df)
    

    Output:

       BALANCE CREDIT_BALANCE  limit_usage
    0      100     things(10)         10.0
    1      255     things(15)         17.0