我遇到过这样一种情况:我试图从一个数据帧中选择一些场景。下面的代码是我目前使用的代码:
dfWater1 = left_merged.loc[left_merged.BVG_2M.isin(['34']) and left_merged.VHC_SC.isin(['6. Nil veg']) and left_merged.wetland.isin(['Estuarine wetlands (e.g. mangroves).', 'Lacustrine wetland (e.g. lake).']) | left_merged.RE.isin(['water', 'reef', 'ocean', 'estuary', 'canal'])].copy()
或者,使用一些额外的括号来包含AND和分隔OR:
dfWater1 = left_merged.loc[(left_merged.BVG_2M.isin(['34']) and left_merged.VHC_SC.isin(['6. Nil veg']) and left_merged.wetland.isin(['Estuarine wetlands (e.g. mangroves).', 'Lacustrine wetland (e.g. lake).'])) | (left_merged.RE.isin(['water', 'reef', 'ocean', 'estuary', 'canal']))].copy()
基本上,我要求在以下位置选择行:
(
Column BVG_2M = 34
AND
Column VHC_SC = '6. Nil veg'
AND
Column wetland is one of the following ['Estuarine wetlands (e.g. mangroves).', 'Lacustrine wetland (e.g. lake).']
)
OR
(
Column RE is one of the following ['water', 'reef', 'ocean', 'estuary', 'canal']
)
数据集非常大,所以我想尽量保持选择的速度(因此使用.loc并以矢量化的方式处理),如果可能的话,尽量避免创建超过保留内存所需的数据帧。
我认为,我真正的问题是,我不确定如何构造.loc语句,甚至不确定我是否可以这样做。
错误消息
File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\pandas\core\generic.py", line 1479, in __nonzero__
f"The truth value of a {type(self).__name__} is ambiguous. "
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
发布于 2020-10-23 09:14:45
您应该使用&
而不是and
,并用括号将每个条件括起来。在新行上设置格式,并将所有内容对齐,这也有助于防止括号错误:
dfWater1 = left_merged.loc[((left_merged.BVG_2M.isin(['34'])) &
(left_merged.VHC_SC.isin(['6. Nil veg'])) &
(left_merged.wetland.isin(['Estuarine wetlands (e.g. mangroves).', 'Lacustrine wetland (e.g. lake).'])))
| (left_merged.RE.isin(['water', 'reef', 'ocean', 'estuary', 'canal']))].copy()
https://stackoverflow.com/questions/64492454
复制相似问题