类型错误:尝试查找数据框中 2 列的比率时不支持 /: 'float' 和 'method' 的操作数类型



我正在尝试查找数据帧中 2 列的比率并将其存储在同一数据帧的新列中,同时这样做我遇到了以下错误类型错误:不支持的操作数类型/:"浮点"和"方法"。

我的数据集有近 8950 行和 21 列,我已经删除了所有 NAN。

>> 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

当我试图找到比率时,我遇到了错误。 错误字符串和代码如下。

>> credit['LIMIT_USAGE'] = credit['BALANCE']/credit['CREDIT_LIMIT']
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~AppDataLocalContinuumanaconda3libsite-packagespandascoreops__init__.py in na_op(x, y)
967         try:
--> 968             result = expressions.evaluate(op, str_rep, x, y, **eval_kwargs)
969         except TypeError:
~AppDataLocalContinuumanaconda3libsite-packagespandascorecomputationexpressions.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)
~AppDataLocalContinuumanaconda3libsite-packagespandascorecomputationexpressions.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 
~AppDataLocalContinuumanaconda3libsite-packagespandascorecomputationexpressions.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']
~AppDataLocalContinuumanaconda3libsite-packagespandascoreops__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
~AppDataLocalContinuumanaconda3libsite-packagespandascoreops__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)
~AppDataLocalContinuumanaconda3libsite-packagespandascoreops__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'

我也尝试使用应用功能,即使不起作用。 应用的代码和错误粘贴在下面

>> 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)
~AppDataLocalContinuumanaconda3libsite-packagespandascoreframe.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):
~AppDataLocalContinuumanaconda3libsite-packagespandascoreapply.py in get_result(self)
184             return self.apply_raw()
185 
--> 186         return self.apply_standard()
187 
188     def apply_empty_result(self):
~AppDataLocalContinuumanaconda3libsite-packagespandascoreapply.py in apply_standard(self)
290 
291         # compute the result using the series generator
--> 292         self.apply_series_generator()
293 
294         # wrap results
~AppDataLocalContinuumanaconda3libsite-packagespandascoreapply.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')

请为此提出快速解决方案。

提前谢谢。

只需对代码稍作修改即可使其工作。

>>> 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
>>>

尝试这样的事情,看看是否有所作为。


上面的 Apply-函数仅在两个值都可以相除时才有效。

如果你有一个自定义类,那么你可以做这样的事情来使其工作:

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)

输出:

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

相关内容

最新更新