Home > Backend Development > Python Tutorial > How to Create a Column Based on If-Else-Else Conditions in Pandas?

How to Create a Column Based on If-Else-Else Conditions in Pandas?

DDD
Release: 2024-10-20 06:55:29
Original
268 people have browsed it

How to Create a Column Based on If-Else-Else Conditions in Pandas?

Creating a Column with If-Else-Else Conditions in Pandas

To create a new column based on an if-elif-else condition, there are two main approaches:

Non-Vectorized Approach

This approach involves defining a function that operates on rows:

<code class="python">def f(row):
    if row['A'] == row['B']:
        val = 0
    elif row['A'] > row['B']:
        val = 1
    else:
        val = -1
    return val</code>
Copy after login

Then, apply it to the dataframe along the rows:

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

Vectorized Approach

The vectorized approach utilizes np.where to create the new column directly:

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

This approach is more efficient for large datasets.

Here's an example using the provided dataframe:

Input DataFrame

A B
2 2
3 1
1 3

Output DataFrame

A B C
2 2 0
3 1 1
1 3 -1

The above is the detailed content of How to Create a Column Based on If-Else-Else Conditions in Pandas?. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template