Pandas¶
Pandas 는 엑셀과 비슷한 역할을 하는 Python의 주요한 라이브러리 입니다. 데이터를 읽고, 처리하고, 분석할 수 있습니다.
- Pandas 라이브러리를 이용해서 엑셀 형식의 파일 (Spreadsheet, ex. csv, xlsx, ...) 을 열어봅니다.
- DataFrame를 사용하는 법을 배워봅니다.
1. 파일 로드하고 살펴보기¶
- 경로 (Path) = 파일 위치 & 파일 이름
/: root. Windows에서는 기본옵션에서 C:\~/: 사용자 폴더. Windows에서는 C:\Users\사용자계정이름./: 현재 작업 폴더(working directory), 별도로 작업하지 않은 경우 생략 가능../: 현재 폴더의 상위 폴더
- Windows 에는 "한글" 을 조심할 것
In [ ]:
Copied!
# print working directory
%pwd
# print working directory
%pwd
In [ ]:
Copied!
# pandas 설치
%pip install -U pandas
# pandas 설치
%pip install -U pandas
In [12]:
Copied!
import pandas as pd
df = pd.read_csv("./AAPL_data.csv")
df
import pandas as pd
df = pd.read_csv("./AAPL_data.csv")
df
Out[12]:
| Price | Close | High | Low | Open | Volume | |
|---|---|---|---|---|---|---|
| 0 | Ticker | AAPL | AAPL | AAPL | AAPL | AAPL |
| 1 | Date | NaN | NaN | NaN | NaN | NaN |
| 2 | 2023-01-03 | 123.7684555053711 | 129.53777972145332 | 122.87781986866916 | 128.92423659950236 | 112117500 |
| 3 | 2023-01-04 | 125.04503631591797 | 127.32110440506466 | 123.77835783326215 | 125.56951966733021 | 89113600 |
| 4 | 2023-01-05 | 123.71897888183594 | 126.44036106917035 | 123.46169000194207 | 125.80702181866339 | 80962700 |
| ... | ... | ... | ... | ... | ... | ... |
| 247 | 2023-12-22 | 192.6561737060547 | 194.45734722393942 | 192.02924020237552 | 194.22845757962483 | 37122800 |
| 248 | 2023-12-26 | 192.10885620117188 | 192.94475743470915 | 191.88992751842636 | 192.66612369019674 | 28919300 |
| 249 | 2023-12-27 | 192.20835876464844 | 192.5566585360189 | 190.15840400250286 | 191.55158790358445 | 48087700 |
| 250 | 2023-12-28 | 192.6362762451172 | 193.71101293849657 | 192.22827139973785 | 193.1935437488545 | 34049900 |
| 251 | 2023-12-29 | 191.5913848876953 | 193.452263485871 | 190.7952819760843 | 192.9547010641641 | 42628800 |
252 rows × 6 columns
In [13]:
Copied!
df = df[2:]
df
df = df[2:]
df
Out[13]:
| Price | Close | High | Low | Open | Volume | |
|---|---|---|---|---|---|---|
| 2 | 2023-01-03 | 123.7684555053711 | 129.53777972145332 | 122.87781986866916 | 128.92423659950236 | 112117500 |
| 3 | 2023-01-04 | 125.04503631591797 | 127.32110440506466 | 123.77835783326215 | 125.56951966733021 | 89113600 |
| 4 | 2023-01-05 | 123.71897888183594 | 126.44036106917035 | 123.46169000194207 | 125.80702181866339 | 80962700 |
| 5 | 2023-01-06 | 128.27110290527344 | 128.93412872931725 | 123.59032994150238 | 124.6986773645302 | 87754700 |
| 6 | 2023-01-09 | 128.7955780029297 | 132.02166222689684 | 128.53828914886986 | 129.11225514638548 | 70790800 |
| ... | ... | ... | ... | ... | ... | ... |
| 247 | 2023-12-22 | 192.6561737060547 | 194.45734722393942 | 192.02924020237552 | 194.22845757962483 | 37122800 |
| 248 | 2023-12-26 | 192.10885620117188 | 192.94475743470915 | 191.88992751842636 | 192.66612369019674 | 28919300 |
| 249 | 2023-12-27 | 192.20835876464844 | 192.5566585360189 | 190.15840400250286 | 191.55158790358445 | 48087700 |
| 250 | 2023-12-28 | 192.6362762451172 | 193.71101293849657 | 192.22827139973785 | 193.1935437488545 | 34049900 |
| 251 | 2023-12-29 | 191.5913848876953 | 193.452263485871 | 190.7952819760843 | 192.9547010641641 | 42628800 |
250 rows × 6 columns
In [ ]:
Copied!
df.head()
df.head()
In [ ]:
Copied!
df.tail(n=10)
df.tail(n=10)
In [ ]:
Copied!
df.shape
df.shape
In [ ]:
Copied!
df.index
df.index
In [ ]:
Copied!
df.columns
df.columns
In [14]:
Copied!
df.dtypes
df.dtypes
Out[14]:
Price object Close object High object Low object Open object Volume object dtype: object
2. 데이터 다루기¶
- 데이터 타입 설정하기
- 데이터 선택하기, 정렬하기
- 데이터 선택을 위한 조건 생성하고 그대로 사용하기
애플은 1년동안 며칠이나 상승을 했고, 며칠이나 하락을 했을까요?
In [ ]:
Copied!
df.sample(n=10)
df.sample(frac=0.1)
df.sample(n=10)
df.sample(frac=0.1)
In [24]:
Copied!
df.loc[:, "Volume"] = df["Volume"].astype('float')
df.loc[:, "Volume"] = df["Volume"].astype('float')
In [19]:
Copied!
df.nlargest(10,"Volume")
df.nlargest(10,"Volume")
Out[19]:
| Price | Close | High | Low | Open | Volume | |
|---|---|---|---|---|---|---|
| 24 | 2023-02-03 | 152.89219665527344 | 155.74223078414798 | 146.29160978319123 | 146.4895254643634 | 154357300.0 |
| 242 | 2023-12-15 | 196.6068115234375 | 197.43275173465906 | 196.0395831061416 | 196.5669980286803 | 128256700.0 |
| 107 | 2023-06-05 | 178.2287139892578 | 183.55830143833987 | 176.70029356551757 | 181.25576664565278 | 121946500.0 |
| 23 | 2023-02-02 | 149.25050354003906 | 149.60674271507267 | 146.62807162382762 | 147.35047067319957 | 118339000.0 |
| 149 | 2023-08-04 | 180.62060546875 | 185.97004732736383 | 180.5511249208849 | 184.12404245746146 | 115799700.0 |
| 87 | 2023-05-05 | 172.0260009765625 | 172.74950296676494 | 169.24098486047572 | 169.45902904213452 | 113316400.0 |
| 172 | 2023-09-07 | 176.46188354492188 | 177.10787273977496 | 172.46674085776903 | 174.0965977246818 | 112488800.0 |
| 2 | 2023-01-03 | 123.7684555053711 | 129.53777972145332 | 122.87781986866916 | 128.92423659950236 | 112117500.0 |
| 178 | 2023-09-15 | 173.92764282226562 | 175.40843335624928 | 172.74501513151833 | 175.38855280048267 | 109205100.0 |
| 116 | 2023-06-16 | 183.52854919433594 | 185.58298054193108 | 182.88344624177174 | 185.32492724572717 | 101235600.0 |
In [20]:
Copied!
df.sort_values('Volume')
df.sort_values('Volume')
Out[20]:
| Price | Close | High | Low | Open | Volume | |
|---|---|---|---|---|---|---|
| 227 | 2023-11-24 | 189.0438690185547 | 189.96932784078396 | 188.3273779116158 | 189.93947531002817 | 24048300.0 |
| 248 | 2023-12-26 | 192.10885620117188 | 192.94475743470915 | 191.88992751842636 | 192.66612369019674 | 28919300.0 |
| 126 | 2023-07-03 | 191.0117950439453 | 192.4211080946957 | 190.3170502473471 | 192.32185451119005 | 31458200.0 |
| 250 | 2023-12-28 | 192.6362762451172 | 193.71101293849657 | 192.22827139973785 | 193.1935437488545 | 34049900.0 |
| 146 | 2023-08-01 | 194.1381072998047 | 195.24967486566203 | 193.81058861115622 | 194.7633716275856 | 35175100.0 |
| ... | ... | ... | ... | ... | ... | ... |
| 149 | 2023-08-04 | 180.62060546875 | 185.97004732736383 | 180.5511249208849 | 184.12404245746146 | 115799700.0 |
| 23 | 2023-02-02 | 149.25050354003906 | 149.60674271507267 | 146.62807162382762 | 147.35047067319957 | 118339000.0 |
| 107 | 2023-06-05 | 178.2287139892578 | 183.55830143833987 | 176.70029356551757 | 181.25576664565278 | 121946500.0 |
| 242 | 2023-12-15 | 196.6068115234375 | 197.43275173465906 | 196.0395831061416 | 196.5669980286803 | 128256700.0 |
| 24 | 2023-02-03 | 152.89219665527344 | 155.74223078414798 | 146.29160978319123 | 146.4895254643634 | 154357300.0 |
250 rows × 6 columns
In [7]:
Copied!
df["Close"]
df["Close"]
Out[7]:
0 AAPL
1 NaN
2 123.7684555053711
3 125.04503631591797
4 123.71897888183594
...
247 192.6561737060547
248 192.10885620117188
249 192.20835876464844
250 192.6362762451172
251 191.5913848876953
Name: Close, Length: 252, dtype: object
In [ ]:
Copied!
In [25]:
Copied!
df.loc[:,"Close"] = df["Close"].astype('float')
df.loc[:,"Open"] = df['Open'].astype('float')
df['Close'] - df['Open']
df.loc[:,"Close"] = df["Close"].astype('float')
df.loc[:,"Open"] = df['Open'].astype('float')
df['Close'] - df['Open']
Out[25]:
2 -5.155781
3 -0.524483
4 -2.088043
5 3.572426
6 -0.316677
...
247 -1.572284
248 -0.557267
249 0.656771
250 -0.557268
251 -1.363316
Length: 250, dtype: float64
In [26]:
Copied!
df["Close"] > df['Open']
df["Close"] > df['Open']
Out[26]:
2 False
3 False
4 False
5 True
6 False
...
247 False
248 False
249 True
250 False
251 False
Length: 250, dtype: bool
In [27]:
Copied!
df[df["Close"] > df['Open']]
df[df["Close"] > df['Open']]
Out[27]:
| Price | Close | High | Low | Open | Volume | |
|---|---|---|---|---|---|---|
| 5 | 2023-01-06 | 128.271103 | 128.93412872931725 | 123.59032994150238 | 124.698677 | 87754700.0 |
| 7 | 2023-01-10 | 129.369583 | 129.89406659487034 | 126.78674290984911 | 128.904473 | 63896200.0 |
| 8 | 2023-01-11 | 132.100845 | 132.12062633545213 | 129.1023781585197 | 129.884150 | 69458900.0 |
| 10 | 2023-01-13 | 133.357620 | 133.51595882992456 | 130.28988932008468 | 130.656034 | 57809700.0 |
| 11 | 2023-01-17 | 134.525345 | 135.86128703391867 | 132.73418300243557 | 133.426895 | 63646600.0 |
| ... | ... | ... | ... | ... | ... | ... |
| 240 | 2023-12-13 | 196.994919 | 197.03471713546693 | 193.90007998207173 | 194.138900 | 70404200.0 |
| 241 | 2023-12-14 | 197.144165 | 198.6467979467734 | 195.20367481138322 | 197.054607 | 66831600.0 |
| 242 | 2023-12-15 | 196.606812 | 197.43275173465906 | 196.0395831061416 | 196.566998 | 128256700.0 |
| 244 | 2023-12-19 | 195.979889 | 195.98983469805682 | 194.9350047944863 | 195.203693 | 40714100.0 |
| 249 | 2023-12-27 | 192.208359 | 192.5566585360189 | 190.15840400250286 | 191.551588 | 48087700.0 |
151 rows × 6 columns
In [ ]:
Copied!
3. 데이터 집계하기¶
- 평균내고, 편차 구하고, 다양한 계산 행하기
In [28]:
Copied!
df.describe()
df.describe()
Out[28]:
| Close | Open | Volume | |
|---|---|---|---|
| count | 250.000000 | 250.000000 | 2.500000e+02 |
| mean | 171.281995 | 170.992146 | 5.921703e+07 |
| std | 17.418788 | 17.615621 | 1.777392e+07 |
| min | 123.718979 | 124.698677 | 2.404830e+07 |
| 25% | 160.670422 | 160.117881 | 4.781208e+07 |
| 50% | 174.389793 | 174.161200 | 5.507750e+07 |
| 75% | 186.265335 | 185.399351 | 6.574292e+07 |
| max | 197.144165 | 197.054607 | 1.543573e+08 |
In [31]:
Copied!
df[["Close", "Open", "Volume"]].corr()
df[["Close", "Open", "Volume"]].corr()
Out[31]:
| Close | Open | Volume | |
|---|---|---|---|
| Close | 1.000000 | 0.994800 | -0.321075 |
| Open | 0.994800 | 1.000000 | -0.324514 |
| Volume | -0.321075 | -0.324514 | 1.000000 |
In [34]:
Copied!
df["date"] = pd.to_datetime(df['Price'])
df['weekday'] = df['date'].dt.weekday
df
df["date"] = pd.to_datetime(df['Price'])
df['weekday'] = df['date'].dt.weekday
df
/var/folders/kx/c6xk17ln6blbs7f3nf_p1str0000gn/T/ipykernel_8397/3916596524.py:1: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df["date"] = pd.to_datetime(df['Price']) /var/folders/kx/c6xk17ln6blbs7f3nf_p1str0000gn/T/ipykernel_8397/3916596524.py:2: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df['weekday'] = df['date'].dt.weekday
Out[34]:
| Price | Close | High | Low | Open | Volume | date | weekday | |
|---|---|---|---|---|---|---|---|---|
| 2 | 2023-01-03 | 123.768456 | 129.53777972145332 | 122.87781986866916 | 128.924237 | 112117500.0 | 2023-01-03 | 1 |
| 3 | 2023-01-04 | 125.045036 | 127.32110440506466 | 123.77835783326215 | 125.569520 | 89113600.0 | 2023-01-04 | 2 |
| 4 | 2023-01-05 | 123.718979 | 126.44036106917035 | 123.46169000194207 | 125.807022 | 80962700.0 | 2023-01-05 | 3 |
| 5 | 2023-01-06 | 128.271103 | 128.93412872931725 | 123.59032994150238 | 124.698677 | 87754700.0 | 2023-01-06 | 4 |
| 6 | 2023-01-09 | 128.795578 | 132.02166222689684 | 128.53828914886986 | 129.112255 | 70790800.0 | 2023-01-09 | 0 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 247 | 2023-12-22 | 192.656174 | 194.45734722393942 | 192.02924020237552 | 194.228458 | 37122800.0 | 2023-12-22 | 4 |
| 248 | 2023-12-26 | 192.108856 | 192.94475743470915 | 191.88992751842636 | 192.666124 | 28919300.0 | 2023-12-26 | 1 |
| 249 | 2023-12-27 | 192.208359 | 192.5566585360189 | 190.15840400250286 | 191.551588 | 48087700.0 | 2023-12-27 | 2 |
| 250 | 2023-12-28 | 192.636276 | 193.71101293849657 | 192.22827139973785 | 193.193544 | 34049900.0 | 2023-12-28 | 3 |
| 251 | 2023-12-29 | 191.591385 | 193.452263485871 | 190.7952819760843 | 192.954701 | 42628800.0 | 2023-12-29 | 4 |
250 rows × 8 columns
In [38]:
Copied!
df.groupby("weekday")[['Open', 'Close', 'Volume']].mean()
df.groupby("weekday")[['Open', 'Close', 'Volume']].mean()
Out[38]:
| Open | Close | Volume | |
|---|---|---|---|
| weekday | |||
| 0 | 171.471788 | 171.955209 | 5.626837e+07 |
| 1 | 170.407503 | 170.581198 | 5.477188e+07 |
| 2 | 170.997955 | 170.949820 | 5.886871e+07 |
| 3 | 170.648236 | 170.965807 | 6.017473e+07 |
| 4 | 171.491563 | 172.043658 | 6.566139e+07 |