■ DataFrame 클래스에서 [] 연산자를 사용해 양수 값을 음수 값으로 변경하는 방법을 보여준다.
▶ main.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import pandas as pd import numpy as np datetimeIndex = pd.date_range("20130101", periods = 6) dataFrame = pd.DataFrame(np.random.randn(6, 4), index = datetimeIndex, columns = list("ABCD")) print(dataFrame) """ A B C D 2013-01-01 -2.415582 -0.520550 -1.801407 0.689429 2013-01-02 0.945648 1.536237 -1.778640 0.202120 2013-01-03 0.568660 1.233407 -1.016520 0.733999 2013-01-04 -0.290816 1.417348 -0.050532 -0.024839 2013-01-05 -0.611079 0.372267 1.136837 -2.772960 2013-01-06 2.101773 -0.337019 1.035544 -1.979480 """ print() dataFrame[dataFrame > 0] = -dataFrame print(dataFrame) """ A B C D 2013-01-01 -2.415582 -0.520550 -1.801407 -0.689429 2013-01-02 -0.945648 -1.536237 -1.778640 -0.202120 2013-01-03 -0.568660 -1.233407 -1.016520 -0.733999 2013-01-04 -0.290816 -1.417348 -0.050532 -0.024839 2013-01-05 -0.611079 -0.372267 -1.136837 -2.772960 2013-01-06 -2.101773 -0.337019 -1.035544 -1.979480 """ |
▶ requirements.txt
1 2 3 4 5 6 7 8 |
numpy==2.1.2 pandas==2.2.3 python-dateutil==2.9.0.post0 pytz==2024.2 six==1.16.0 tzdata==2024.2 |
※ pip install pandas 명령을 실행했다.