[Pandas] DataFrame에서 조건을 사용해 값 만들거나 수정하기


환경

  • Python
  • Pandas


사용법

  • 조건에 따라 기존 값들 변경하기
  • 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
  • 조건을 사용해 새로운 컬럼 생성하기
  • 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


참고자료