Home >Backend Development >Python Tutorial >How to Use NumPy \'where\' with Multiple Conditions for Conditional Operations?

How to Use NumPy \'where\' with Multiple Conditions for Conditional Operations?

Linda Hamilton
Linda HamiltonOriginal
2024-10-19 12:59:02551browse

How to Use NumPy

Using NumPy "where" with Multiple Conditions

When working with data manipulation tasks, it's often necessary to apply different conditions to select or modify specific values. NumPy's "where" function is a powerful tool for conditional operations, but by default, it only handles two conditions.

Consider the case where you want to add a new column, "energy_class," to a pandas DataFrame called "df_energy." The energy class should be assigned based on the following conditions:

  • "consumption_energy" values greater than 400: "high"
  • "consumption_energy" values between 200 and 400: "medium"
  • "consumption_energy" values less than or equal to 200: "low"

Since NumPy's "where" function doesn't support multiple conditions, the solution lies in using its more powerful sibling, "select." Here's how to approach this problem:

import numpy as np

# Assuming df_energy has a column called "consumption_energy"
col = 'consumption_energy'
conditions = [ df_energy[col] >= 400,
             (df_energy[col] < 400) & (df_energy[col] > 200),
             df_energy[col] <= 200 ]
choices = [ "high", 'medium', 'low' ]

# Create the "energy_class" column using np.select
df_energy["energy_class"] = np.select(conditions, choices, default=np.nan)

This extended usage of "select" allows you to define multiple conditions and map corresponding choices to the output. By default, it assigns "np.nan" if none of the conditions are met.

As a result, your DataFrame will now have the "energy_class" column populated with the appropriate labels based on your specified conditions, providing a clear classification of energy consumption levels.

The above is the detailed content of How to Use NumPy \'where\' with Multiple Conditions for Conditional Operations?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn