使用Numpy 的「where」應用多個條件
使用NumPy 的「where」函數可以成為有條件選擇數組中元素的強大工具基於具體標準。然而,「where」的標準實作僅允許兩個條件具有相應的輸出。在處理涉及多個條件的場景時,這可能會成為一個限制。
為了解決這個問題,更通用的解決方案是使用「np.select」函數。 「np.select」允許同時評估多個條件並分配相應的輸出。讓我們探討如何將其應用於根據消耗能源值將能源類別分配給 DataFrame 的問題。
實作:
col = 'consumption_energy' conditions = [ df['consumption_energy'] >= 400, (df['consumption_energy'] 200), df['consumption_energy'] <p>此程式碼建立了三個基於'conspiration_energy' 欄位中的值的條件:</p><ol> <li>' conspiration_energy' >= 400:將此條件指定為'high'。 </li> <li>'conspiration_energy' </li>第400章200:將此條件指定為「中」。 <li>「conspiration_energy」<p></p>「np.select」函數評估每個條件,如果滿足任何條件,它會從「選擇」清單中分配相應的輸出。如果沒有滿足任何條件,它將指定“nan”作為預設值。 <p><strong></strong>輸出:</p> <pre class="brush:php;toolbar:false"> consumption_energy energy_class 0 459 high 1 416 high 2 186 low 3 250 medium 4 411 high 5 210 medium 6 343 medium 7 328 medium 8 208 medium 9 223 medium透過利用“np.select”,我們已根據指定條件成功將能量類別分配給DataFrame,提供了在選擇數組中的元素時處理多個條件的通用方法。
以上是如何使用 NumPy 的「np.select」在陣列中套用多個條件?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

ForhandlinglargedatasetsinPython,useNumPyarraysforbetterperformance.1)NumPyarraysarememory-efficientandfasterfornumericaloperations.2)Avoidunnecessarytypeconversions.3)Leveragevectorizationforreducedtimecomplexity.4)Managememoryusagewithefficientdata

Inpython,ListSusedynamicMemoryAllocationWithOver-Asalose,而alenumpyArraySallaySallocateFixedMemory.1)listssallocatemoremoremoremorythanneededinentientary上,respizeTized.2)numpyarsallaysallaysallocateAllocateAllocateAlcocateExactMemoryForements,OfferingPrediCtableSageButlessemageButlesseflextlessibility。

Inpython,YouCansspecthedatatAtatatPeyFelemereModeRernSpant.1)Usenpynernrump.1)Usenpynyp.dloatp.dloatp.ploatm64,formor professisconsiscontrolatatypes。

NumPyisessentialfornumericalcomputinginPythonduetoitsspeed,memoryefficiency,andcomprehensivemathematicalfunctions.1)It'sfastbecauseitperformsoperationsinC.2)NumPyarraysaremorememory-efficientthanPythonlists.3)Itoffersawiderangeofmathematicaloperation

Contiguousmemoryallocationiscrucialforarraysbecauseitallowsforefficientandfastelementaccess.1)Itenablesconstanttimeaccess,O(1),duetodirectaddresscalculation.2)Itimprovescacheefficiencybyallowingmultipleelementfetchespercacheline.3)Itsimplifiesmemorym

SlicingaPythonlistisdoneusingthesyntaxlist[start:stop:step].Here'showitworks:1)Startistheindexofthefirstelementtoinclude.2)Stopistheindexofthefirstelementtoexclude.3)Stepistheincrementbetweenelements.It'susefulforextractingportionsoflistsandcanuseneg

numpyallowsforvariousoperationsonArrays:1)basicarithmeticlikeaddition,減法,乘法和division; 2)evationAperationssuchasmatrixmultiplication; 3)element-wiseOperations wiseOperationswithOutexpliitloops; 4)

Arresinpython,尤其是Throughnumpyandpandas,weessentialFordataAnalysis,offeringSpeedAndeffied.1)NumpyArseNable efflaysenable efficefliceHandlingAtaSetSetSetSetSetSetSetSetSetSetSetsetSetSetSetSetsopplexoperationslikemovingaverages.2)


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

SublimeText3漢化版
中文版,非常好用

WebStorm Mac版
好用的JavaScript開發工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器