데이터셋을 여러 차례 변형해야 하는 경우 분석에는 전혀 필요 없는 임시 변수를 계속 생성하는
상황이 발생한다. 다음 예제를 살펴보자.
df
=
load
_
data
()
df2
=
df
[
df
['
col2
']
<
0
]
df2
['
col1
_
demeaned
']
=
df2
['
col1
']
-
df2
['
col1
'].
mean
()
result
=
df2
.
groupby
('
key
').
col1
_
demeaned
.
std
()
여기서 실제 데이터를 사용하지는 않지만 새로운 메서드 몇 가지를 만나게 되는데 그중 하나는
df
[
k
] =
v
처럼 컬럼에 값을 대입하는 함수형
DataFrame
.
assign
메서드다. 객체를 변경하는
대신 값 대입이 완료된 새로운
DataFrame
을 반환한다. 아래 두 코드는 동일하다.
#
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.