How to Create a Conditional Column Based on Multiple Conditions in a DataFrame Using Python?

Linda Hamilton
Release: 2024-10-20 06:52:02
Original
677 people have browsed it

How to Create a Conditional Column Based on Multiple Conditions in a DataFrame Using Python?

Creating a Conditional Column Based on Multiple Conditions

As mentioned in the given thread, the task at hand is to generate a new column in a DataFrame based on specific conditions. The DataFrame contains two columns, 'A' and 'B', and the desired column, 'C', should be assigned values based on comparisons between 'A' and 'B'.

The conditions are as follows:

  • If 'A' equals 'B', set 'C' to 0.
  • If 'A' is greater than 'B', set 'C' to 1.
  • If 'A' is less than 'B', set 'C' to -1.

To accomplish this, a Python function can be created to evaluate the conditions and assign the appropriate value to 'C' for each row in the DataFrame. The apply() method can be used to apply the function to each row, passing in the 'axis=1' argument to specify that the function should operate on the rows. The code below demonstrates this approach:

<code class="python">def conditional_value(row):
    if row['A'] == row['B']:
        return 0
    elif row['A'] > row['B']:
        return 1
    else:
        return -1

df['C'] = df.apply(conditional_value, axis=1)</code>
Copy after login

This function-based approach provides a readable and customizable way to create the conditional column.

Alternatively, for better performance on large datasets, a vectorized operation can be used:

<code class="python">df['C'] = np.where(
    df['A'] == df['B'], 0, np.where(
    df['A'] > df['B'], 1, -1))</code>
Copy after login

Here, the np.where() function is used to evaluate the conditions and assign the corresponding values to 'C' efficiently.

The above is the detailed content of How to Create a Conditional Column Based on Multiple Conditions in a DataFrame Using Python?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!