使用Python+Pandas计算满足其他列中2个条件的值

2024-06-16 10:18:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个pandas数据框,我试图找到满足两个条件的行,然后计算一个值

基本前提是:

  • 使用数据帧df
    1. 将调整内重量燃油中的值替换为预清洁燃油调整 当:
  • 1.1预清洁燃料调节=12;及
  • 1.2调整内部重量燃料<;=1200
    1. 然后将输入值乘以100(当前以2位小数浮点) 位置,需要为int)

我需要所有其他行保持不变并保持在数据帧中

目前,我正在尝试使用以下内容:

df['ADJ_INT_WEIGHT_FUEL'] = (df['PreClear_Fuel_Adj']*100).where(df['PreClear_Fuel_Adj'] == 12 & df['ADJ_INT_WEIGHT_FUEL'] <= 1200)

示例
原创的

+---------------------+-------------------+
| ADJ_INT_WEIGHT_FUEL | PreClear_Fuel_Adj |
+---------------------+-------------------+
|                  10 |                 0 |
|                  30 |                12 | <-- Will be identified by .where()
|                  20 |                 0 |
|                   5 |                 0 |
|                  15 |                12 | <-- Will be identified by .where()
|                  25 |                 0 |
|                3500 |                12 |
+---------------------+-------------------+

算计

+---------------------+-------------------+
| ADJ_INT_WEIGHT_FUEL | PreClear_Fuel_Adj |
+---------------------+-------------------+
|                  10 |                 0 |
|                1200 |                12 | <-- Was identified by .where()
|                  20 |                 0 |
|                   5 |                 0 |
|                1200 |                12 | <-- Was identified by .where()
|                  25 |                 0 |
|                3500 |                12 |
+---------------------+-------------------+

问题 我目前收到一个错误。Pandas正在ArcGIS Pro中使用,因此下面的转储中也会出现一些ArcGIS Pro错误消息。但是,一般来说,我们可以将此示例视为通用数据帧

要么我对数据类型有问题,要么我对使用&;操作人员也就是说,我不确定我的选择和计算语法是否正确。因此,如果有错误,我可以使用一些反馈

谢谢

Traceback (most recent call last):
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\site-packages\pandas\core\ops\array_ops.py", line 274, in na_logical_op
    result = op(x, y)
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\site-packages\pandas\core\ops\roperator.py", line 52, in rand_
    return operator.and_(right, left)
**TypeError: ufunc 'bitwise_and' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''**

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\site-packages\pandas\core\ops\array_ops.py", line 288, in na_logical_op
    result = libops.scalar_binop(x, y, op)
  File "pandas\_libs\ops.pyx", line 169, in pandas._libs.ops.scalar_binop
**ValueError: Buffer dtype mismatch, expected 'Python object' but got 'double'**

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 479, in execute
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\site-packages\pandas\core\ops\common.py", line 64, in new_method
    return method(self, other)
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\site-packages\pandas\core\ops\__init__.py", line 552, in wrapper
    res_values = logical_op(lvalues, rvalues, op)
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\site-packages\pandas\core\ops\array_ops.py", line 366, in logical_op
    res_values = na_logical_op(lvalues, rvalues, op)
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\site-packages\pandas\core\ops\array_ops.py", line 298, in na_logical_op
    f"Cannot perform '{op.__name__}' with a dtyped [{x.dtype}] array "
TypeError: Cannot perform 'rand_' with a dtyped [float64] array and scalar of type [bool]
 Failed to execute (Tool).


Tags: inpandasbinliblinepy3filesprogram
2条回答

试一试

m = (df.PreClear_Fuel_Adj == 12) & (df.ADJ_INT_WEIGHT_FUEL <= 1200)
df['ADJ_INT_WEIGHT_FUEL'] *= m.astype(int)*100

df.loc[m, 'ADJ_INT_WEIGHT_FUEL'] = df['ADJ_INT_WEIGHT_FUEL']*100

使用np.where(condition, answer if condition, answer if not condition)

import numpy as np
df['ADJ_INT_WEIGHT_FUEL']=np.where((df.PreClear_Fuel_Adj == 12)&(df.ADJ_INT_WEIGHT_FUEL <= 1200),\
                          df.PreClear_Fuel_Adj*100,df.ADJ_INT_WEIGHT_FUEL)
print(df)
    



 ADJ_INT_WEIGHT_FUEL     PreClear_Fuel_Adj
0                   10                  0
1                 1200                 12
2                   20                  0
3                    5                  0
4                 1200                 12
5                   25                  0
6                 3500                 12

相关问题 更多 >