• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Python / Python Tips / Pandas create new column using if else condition

Pandas create new column using if else condition

September 9, 2022 by cmdlinetips

In this quick tutorial, we will learn how to create a new column using if else condition on an existing column in a Pandas dataframe.

To add new column using a condional on existing column we will use Numpy’s where function. So, let us load both numby and Pandas to get started.

import pandas as pd
import numpy as np 

We. will use one of the built-in datasets from Seaborn package. Let us load Seaborn and the health expenditure dataset.

import seaborn as sns
healthexp =  sns.load_dataset("healthexp")

Our health expenditure dataset looks like this.

healthexp.head()

	Year	Country	Spending_USD	Life_Expectancy
0	1970	Germany	252.311	70.6
1	1970	France	192.143	72.2
2	1970	Great Britain	123.993	71.9
3	1970	Japan	150.437	72.0
4	1970	USA	326.961	70.9

We will create a new column using the existing Country column with if condition. Our new column has Yes value if the country value for that row is USA, No if it is not. We use Numpy’s where() function to check the country values and create a new column.

healthexp['Is_USA'] = np.where(healthexp["Country"] == 'USA', 
                               "Yes",
                               "No")
healthexp.head()

Year	Country	Spending_USD	Life_Expectancy	USA	Is_USA
0	1970	Germany	252.311	70.6	False	No
1	1970	France	192.143	72.2	False	No
2	1970	Great Britain	123.993	71.9	False	No
3	1970	Japan	150.437	72.0	False	No
4	1970	USA	326.961	70.9	True	Yes

Add new column using i condition on. existing column in Pandas
Add new column using i condition on. existing column in Pandas

In the second example, we create a boolean column based on the value of Country column. If the country column value is USA, we have True else False.


healthexp['Is_USA'] = np.where(healthexp["Country"] == 'USA', True, False)
healthexp.head()

Year	Country	Spending_USD	Life_Expectancy	USA	Is_USA
0	1970	Germany	252.311	70.6	False	False
1	1970	France	192.143	72.2	False	False
2	1970	Great Britain	123.993	71.9	False	False
3	1970	Japan	150.437	72.0	False	False
4	1970	USA	326.961	70.9	True	True

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default ThumbnailHow To Create a Column Using Condition on Another Column in Pandas? Missing Values Count with isna()How To Get Number of Missing Values in Each Column in Pandas Pandas Filter/Select Rows Based on Column ValuesHow To Filter Pandas Dataframe By Values of Column? Default ThumbnailHow To Insert a Column at Specific Location in Pandas DataFrame?

Filed Under: Pandas 101, Python Tips Tagged With: Pandas new column using if condition

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version