«   2024/07   »
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
Recent Posts
Today
Total
관리 메뉴

짜리몽땅 매거진

[Python] 모듈 패키지 본문

Data/Python

[Python] 모듈 패키지

쿡국 2023. 8. 25. 14:24

모듈 패키지

  • 우리가 당연하게 사용하는 것들이 결국 다 만들어 사용하는 것
  • 직접 우리도 만들 수 있고, 패키지 ,내장 함수를 만들 수 있다.
  • 패키지라고 하는 것은 -> 여러가지 기능 묶은 것들을 말한다.
  • 모듈 특정 기능에 대해서 작성해 둔 것들 -> print 에 대한 기능들

 

  • 대부분 다 만들어진 모듈을 사용하는 것
  • 이 모듈이 내 경로에 없으면 부를 수 없다. 에러가 나게 되고
  • 이 모듈을 부르기 위해서 install 작업 등을 진행해서 편하게 불러올 수 있게 만든다.
  • 처음에 에러가 많이 나는 경우가 패키지가 없는데 불러오는 것에서부터 에러가 많이난다.
  • import 모듈
import 모듈

#수학 모듈 패키지
import math
#수학 모듈 패키지
import math
math.pi #파이값 부르기
3.141592653589793
math.sqrt(36) #제곱근 값 부르기
6.0
import math as ma #간단하게 표현하기 위한 별칭을 지정할 수 있다
ma.pi
3.141592653589793
# 데이터 분석에 필요한 모듈을 한 번 사용해보자

import pandas as pd
import numpy as np
# 데이터 분석에 필요한 모듈을 한 번 사용해보자
​
import pandas as pd
import numpy as np
# 판다스에서 제공하는 함수 pd.DataFrame

info1=pd.DataFrame({'이름':['국준
# 판다스에서 제공하는 함수 pd.DataFrame
​
info1=pd.DataFrame({'이름':['국준호', '국주노', '쿸주노'],'키':[160,170,180]})
info1
이름	키
0	국준호	160
1	국주노	170
2	쿸주노	180
# 넘파이는 공학
​
np.sum(info1['키']) 
510
np.max(info1['키'])
180
np.min(info1['키'])
160
np.mean(info1['키']) #평균
170.0
np.var(info1['키']) #분산
66.66666666666667
np.std(info1['키']) #표준편차
8.16496580927726
np.median(info1['키']) #중앙값
170.0

 

타이타닉 데이터를 모듈에서 불러오자!

  • 데이터를 불러오는 방법은 2가지가 있다.
  • 패키지에서 불러올 수 있는 데이터
  • 내가 직접 만든 또는 다운로드 받은 데이터를 불러올 수 있다. -> 로컬에 있는 환경에서 가지고 오는 것
import seaborn as sb
pip install seaborn
Requirement already satisfied: seaborn in c:\users\rnrwnsgh\anaconda3\lib\site-packages (0.11.2)
Requirement already satisfied: matplotlib>=2.2 in c:\users\rnrwnsgh\anaconda3\lib\site-packages (from seaborn) (3.5.2)
Requirement already satisfied: pandas>=0.23 in c:\users\rnrwnsgh\anaconda3\lib\site-packages (from seaborn) (1.4.4)
Requirement already satisfied: scipy>=1.0 in c:\users\rnrwnsgh\anaconda3\lib\site-packages (from seaborn) (1.9.1)
Requirement already satisfied: numpy>=1.15 in c:\users\rnrwnsgh\anaconda3\lib\site-packages (from seaborn) (1.21.5)
Requirement already satisfied: python-dateutil>=2.7 in c:\users\rnrwnsgh\anaconda3\lib\site-packages (from matplotlib>=2.2->seaborn) (2.8.2)
Requirement already satisfied: packaging>=20.0 in c:\users\rnrwnsgh\anaconda3\lib\site-packages (from matplotlib>=2.2->seaborn) (21.3)
Requirement already satisfied: fonttools>=4.22.0 in c:\users\rnrwnsgh\anaconda3\lib\site-packages (from matplotlib>=2.2->seaborn) (4.25.0)
Requirement already satisfied: cycler>=0.10 in c:\users\rnrwnsgh\anaconda3\lib\site-packages (from matplotlib>=2.2->seaborn) (0.11.0)
Requirement already satisfied: pyparsing>=2.2.1 in c:\users\rnrwnsgh\anaconda3\lib\site-packages (from matplotlib>=2.2->seaborn) (3.0.9)
Requirement already satisfied: pillow>=6.2.0 in c:\users\rnrwnsgh\anaconda3\lib\site-packages (from matplotlib>=2.2->seaborn) (9.2.0)
Requirement already satisfied: kiwisolver>=1.0.1 in c:\users\rnrwnsgh\anaconda3\lib\site-packages (from matplotlib>=2.2->seaborn) (1.4.2)
Requirement already satisfied: pytz>=2020.1 in c:\users\rnrwnsgh\anaconda3\lib\site-packages (from pandas>=0.23->seaborn) (2022.1)
Requirement already satisfied: six>=1.5 in c:\users\rnrwnsgh\anaconda3\lib\site-packages (from python-dateutil>=2.7->matplotlib>=2.2->seaborn) (1.16.0)
Note: you may need to restart the kernel to use updated packages.
df_tt=sb.load_dataset('titanic')
df_tt
survived	pclass	sex	age	sibsp	parch	fare	embarked	class	who	adult_male	deck	embark_town	alive	alone
0	0	3	male	22.0	1	0	7.2500	S	Third	man	True	NaN	Southampton	no	False
1	1	1	female	38.0	1	0	71.2833	C	First	woman	False	C	Cherbourg	yes	False
2	1	3	female	26.0	0	0	7.9250	S	Third	woman	False	NaN	Southampton	yes	True
3	1	1	female	35.0	1	0	53.1000	S	First	woman	False	C	Southampton	yes	False
4	0	3	male	35.0	0	0	8.0500	S	Third	man	True	NaN	Southampton	no	True
...	...	...	...	...	...	...	...	...	...	...	...	...	...	...	...
886	0	2	male	27.0	0	0	13.0000	S	Second	man	True	NaN	Southampton	no	True
887	1	1	female	19.0	0	0	30.0000	S	First	woman	False	B	Southampton	yes	True
888	0	3	female	NaN	1	2	23.4500	S	Third	woman	False	NaN	Southampton	no	False
889	1	1	male	26.0	0	0	30.0000	C	First	man	True	C	Cherbourg	yes	True
890	0	3	male	32.0	0	0	7.7500	Q	Third	man	True	NaN	Queenstown	no	True
891 rows × 15 columns

for i in df_tt:
    print(i)
survived
pclass
sex
age
sibsp
parch
fare
embarked
class
who
adult_male
deck
embark_town
alive
alone
a=[]
for i in df_tt.fare:
    a.append(i)



fare/pclass # 리스트끼리 나누기 불가능!
fare/pclass # 리스트끼리 나누기 불가능!
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_9012\1778285997.py in <module>
----> 1 fare/pclass

TypeError: unsupported operand type(s) for /: 'list' and 'list'

## 따라서 다음과 같이 리스트를 다시 정의
fare=[]
pclass=[]
fare_pclass=[]
​
for i,j in zip(df_tt.fare, df_tt.pclass):
    fare.append(i)
    pclass.append(j)
    fare_pclass.append(i/j)

'Data > Python' 카테고리의 다른 글

[Python] 데이터분석 기초  (0) 2023.09.12
[Python] 텍스트문법 응용  (0) 2023.08.29
[Python] 정규표현식  (0) 2023.08.14
[Python] if, for문 응용한 퀴즈 만들기  (0) 2023.08.03
[Python] 딕셔너리 set/def/lambda  (0) 2023.07.24