Numpy,其中有多个条件,无法将dtyped[object]数组与[bool]类型的标量进行比较

2024-10-01 17:27:50 发布

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

我试图在numpy中执行多个条件,但得到的错误是:cannot compare a dtyped [object] array with a scalar of type [bool]

这是一句话:

d7['CAD'] = np.where(d7['Category'] == 'Stack' & d7['Currency'] == fxratetable['from_currency'],d7['CAD'] * fxratetable['fx_rate'], d7['CAD'])

所有{}都是{},除了外汇汇率{}

另一个想法是,我用Category = Stack查找一个值,但用Currency = from_currency查找多个值

有人能帮忙吗

谢谢

新错误

ValueError: Can only compare identically-labeled Series objects now. 

这是我的新声明

d7['CAD'] = np.where((d7['Category'] == 'Stack') & 
                     (d7['Currency'] == fxratetable['from_currency']),
                     d7['CAD'] * fxratetable['fx_rate'], 
                     d7['CAD'])

d7:

+--------------+----------+----------+
| CAD          | Currency | Category |
+--------------+----------+----------+
| -4350242.355 | GBP      | Stack    |
+--------------+----------+----------+
| 424223.7584  | AUD      | Stack    |
+--------------+----------+----------+

fxratetable:

+---------------+---------+
| from_currency | fx_rate |
+---------------+---------+
| GBP           | 1.367   |
+---------------+---------+
| AUD           | 0.7706  |
+---------------+---------+

期待新的CAD专栏

+----------------+
| CAD (expected) |
+----------------+
| -5948957.275   |
+----------------+
| 326991.5663    |
+----------------+

Tags: fromratestack错误npwherecurrencycompare
3条回答

问题在于布尔表达式的第二部分。大概d7不止两行,否则就不会有这个问题d7['Currency'] == fxratetable['from_currency']逐行比较两个系列,当行用完fxratetable时,它不知道再将d7与什么进行比较

我不知道您是否已开始使用NumPy,或者您所能做的事情是否有限制,但merge语句可以非常轻松地处理这一问题:

# Merge the exchange rate
d7 = pd.merge(d7, fxratetable, left_on='Currency', right_on='from_currency', how='left')
# Find the rows which have a non-NaN exchange rate and multiply
d7['CAD'] = np.where(~np.isnan(d7['fx_rate']), d7['CAD']*d7['fx_rate'], d7['CAD'])

我想这将是你所期望的。我写得很通俗易懂

d7 = pd.DataFrame({'CAD':[-4350242.355,424223.7584],'Currency':['GBP','AUD'],'Category':['Stack','Stack']})
fxratetable = pd.DataFrame({'from_currency':['GBP','AUD'],'fx_rate':[1.367,0.7706 ]})
_condition1 = d7['Category'] == 'Stack'
_condition2 = d7['Currency'] == fxratetable['from_currency']
d7['CAD (expected)'] = np.where(_condition1 & _condition2,d7['CAD'] * fxratetable['fx_rate'],d7['CAD'])
d7

输出 enter image description here

我个人在处理条件时更喜欢DataFrame.apply

d7['CAD'] = d7.apply(lambda row: row.CAD * fxratetable[fxratetable.from_currency == row.Currency].fx_rate.item() \
                                 if row.Category == 'Stack' else row.CAD, axis=1)

fxratetable[fxratetable.from_currency == row.Currency]选择fxratetable中与正确货币对应的行,.fx_rate选择汇率列,.item()给出数字,而不是序列

您可以看到,只有row.Category == 'Stack'条件仍然存在,有问题的序列比较现在是选择的一部分

最后,axis=1用于将函数应用于数据帧行(docu

我还没有对此进行过广泛的测试,所以请告诉我它是否有效

相关问题 更多 >

    热门问题