[Pandas](EN) Create or modify values in DataFrame with condition


Environment and Prerequisite

  • Python
  • Pandas


Usage

  • Modify exist values depends on condition
  • Use df.loc[]
import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3, 4], 'B': [5, 6, 7, 8], 'C': [9, 10, 11, 12]})

# modify values in column 'B' and 'C' based on a condition in column 'A'
df.loc[df['A'] < 3, ['B', 'C']] = 0

df
  • Make new column depends on condition
  • Use np.where()
import pandas as pd
import numpy as np

df = pd.DataFrame({'A': range(0, 30), 'B': range(30, 60)})

# make condition using & or |
cond_1 = (5 <= df['A']) & (df['A'] <= 10)
cond_2 = (15 <= df['A']) & (df['A'] <= 20)

print(np.where( cond_1 | cond_2))

# create a new column C based on values in column A and an if condition
df['A is in 5~10 or 15~20'] = np.where(cond_1 | cond_2, True, False)

df


Reference