728x90
DataFrame 복사에 대해 테스트
Make a copy of this object’s indices and data.
DataFrame.copy(deep=True)
df2 = df
df3 = df[:]
df3 = df.copy() # deep = True (default)
df2 = df
# 복사 테스트 #
df2 = df
df2.tail()
<결과>
394 44.0 4.0 97.0 52.00 2130.0 24.6 82.0 2.0 vw pickup
395 32.0 4.0 135.0 84.00 2295.0 11.6 82.0 1.0 dodge rampage
396 28.0 4.0 120.0 79.00 2625.0 18.6 82.0 1.0 ford ranger
397 31.0 4.0 119.0 82.00 2720.0 19.4 82.0 1.0 chevy s-10
398 NaN NaN NaN NaN NaN NaN NaN NaN NaN
drop
df2.drop(398,inplace=True)
print(df.tail())
df2.tail()
<결과>
mpg cylinders displacement horsepower weight acceleration \
393 27.0 4.0 140.0 86.00 2790.0 15.6
394 44.0 4.0 97.0 52.00 2130.0 24.6
395 32.0 4.0 135.0 84.00 2295.0 11.6
396 28.0 4.0 120.0 79.00 2625.0 18.6
397 31.0 4.0 119.0 82.00 2720.0 19.4
model year origin name
393 82.0 1.0 ford mustang gl
394 82.0 2.0 vw pickup
395 82.0 1.0 dodge rampage
396 82.0 1.0 ford ranger
397 82.0 1.0 chevy s-10
<결과>
mpg cylinders displacement horsepower weight acceleration model year origin name
393 27.0 4.0 140.0 86.00 2790.0 15.6 82.0 1.0 ford mustang gl
394 44.0 4.0 97.0 52.00 2130.0 24.6 82.0 2.0 vw pickup
395 32.0 4.0 135.0 84.00 2295.0 11.6 82.0 1.0 dodge rampage
396 28.0 4.0 120.0 79.00 2625.0 18.6 82.0 1.0 ford ranger
397 31.0 4.0 119.0 82.00 2720.0 19.4 82.0 1.0 chevy s-10
df2에 원본의 주소를 가지고 있기때문에
df2가 df의 398 인덱스를 가진 행이 드랍된 결과를 반환
df3 = df[ : ]
df3 = df[:]
df3.drop(398,inplace=True)
print(df.tail())
df3.tail()
<결과>
경고문이 발생한다.
<결과>
mpg cylinders displacement horsepower weight acceleration model year origin name
393 27.0 4.0 140.0 86.00 2790.0 15.6 82.0 1.0 ford mustang gl
394 44.0 4.0 97.0 52.00 2130.0 24.6 82.0 2.0 vw pickup
395 32.0 4.0 135.0 84.00 2295.0 11.6 82.0 1.0 dodge rampage
396 28.0 4.0 120.0 79.00 2625.0 18.6 82.0 1.0 ford ranger
397 31.0 4.0 119.0 82.00 2720.0 19.4 82.0 1.0 chevy s-10
복사를 한 df3의 398번째의 행만 drop
df3 = df.copy()
df3 = df.copy()
df.drop(398,inplace=True)
print(df.tail())
df3.tail()
<결과>
mpg cylinders displacement horsepower weight acceleration \
393 27.0 4.0 140.0 86.00 2790.0 15.6
394 44.0 4.0 97.0 52.00 2130.0 24.6
395 32.0 4.0 135.0 84.00 2295.0 11.6
396 28.0 4.0 120.0 79.00 2625.0 18.6
397 31.0 4.0 119.0 82.00 2720.0 19.4
model year origin name
393 82.0 1.0 ford mustang gl
394 82.0 2.0 vw pickup
395 82.0 1.0 dodge rampage
396 82.0 1.0 ford ranger
397 82.0 1.0 chevy s-10
<결과>
mpg cylinders displacement horsepower weight acceleration model year origin name
394 44.0 4.0 97.0 52.00 2130.0 24.6 82.0 2.0 vw pickup
395 32.0 4.0 135.0 84.00 2295.0 11.6 82.0 1.0 dodge rampage
396 28.0 4.0 120.0 79.00 2625.0 18.6 82.0 1.0 ford ranger
397 31.0 4.0 119.0 82.00 2720.0 19.4 82.0 1.0 chevy s-10
398 NaN NaN NaN None NaN NaN NaN NaN None
경고문없이 사본만 깔끔하게 drop 되는 것을 볼 수 있다.
데이터프레임을 복사할때 copy() 를 사용하는 것을 권장한다.
댓글