1. 모듈 : 재사용하고자 하는 함수 / 클래스의 집합.
  - 모듈명 : 소스 파일 명이 곧 모듈명이 된다.


 2. 패키지 : 모듈의 집합체로, 모듈을 특정 기준에 따라 모아 놓은 단위를 패키지로 하고, 디렉토리 구조를 사용한다.

    - 패키지(디렉토리) > 모듈(파일)

 

ps. Python 콘솔에서 모듈 정보를 조회할 수 있다.

>>> help('modules')
Please wait a moment while I gather a list of all available modules...
OpenSSL             _uuid               imaplib             reprlib
PIL                 _warnings           imghdr              requests
__future__          _weakref            imp                 resource
_abc                _weakrefset         importlib           rlcompleter
... 중략

 3. 모듈 import 방법 : import 문을 사용하여 호출한다.

import 패키지명.모듈명
import 모듈명

from 패키지명 import 모듈명
from 모듈명 import 클래스명, 함수명 등

from ~ import ~ as 별칭


import sound.effects.echo
sound.effect.echo.echofilter(input, ouput) # import 문만 사용하면, 패키지를 모두 기술해야 한다
 
from sound.effects import echo
echo.echofilter(input, output)        # import 문으로 상위 패키지만 선언하면, 모듈로 호출 기술.

from sound.effects.echo import echofilter
echofilter(input, output)             # import 문으로 모듈까지 선언하면, 함수를 바로 호출 가능.

 

4. 패키지 구조 예
sound/                 # Top-level package
    __init__.py      # 패키지 파일로, 파이썬은 패키지로 인식한다. 패키지 내의 모든 공통적을 사용할 속성등을 정의할 수 있다.

    formats/          # sub-package
        __init__.py

        wavread.py

    effects/
        __init__.py

end.

'Programming Language > Python' 카테고리의 다른 글

[Python] pandas - DataFrame Text 출력  (0) 2022.03.19
[Python] 클래스  (0) 2022.03.14
[Python] 내장함수  (0) 2022.03.14
[Python] 문자열 포맷 출력  (0) 2022.03.14
[Python] 판다스 pands 필터링, 정렬, 랭크  (0) 2022.03.12

참조: https://docs.python.org/3.9/library/functions.html

생소한 함수도 많네요. ㅋ

 

'Programming Language > Python' 카테고리의 다른 글

[Python] 클래스  (0) 2022.03.14
[Python] 모듈과 패키지  (0) 2022.03.14
[Python] 문자열 포맷 출력  (0) 2022.03.14
[Python] 판다스 pands 필터링, 정렬, 랭크  (0) 2022.03.12
[Python] 판다스 pandas 기본  (0) 2022.03.12

 

문자열 포맷 방식

참조: https://docs.python.org/3.9/library/string.html#formatstrings

 

1. % 기호 방식

2. {} 기호 방식

3. f-strings 방식

 

s_val = 'string'
i_val = 123
f_val = 3.141592

# 1. % 기호방식
for i in range(3):
    print('%% method: %s / %d / %.2f' % (s_val, i_val+i, f_val))
# % method: string / 123 / 3.14
# % method: string / 124 / 3.14
# % method: string / 125 / 3.14

# 2. {} 기호방식
for i in range(3):
    print('{{}} method: {} / {} / {:.2f}'.format(s_val, i_val+i, f_val))
# {} method: string / 123 / 3.14
# {} method: string / 124 / 3.14
# {} method: string / 125 / 3.14

# 3. f-string 방식
for i in range(3):
    print(f'f-string method: {s_val} / {i_val+i} / {f_val:.2f}')
# f-string method: string / 123 / 3.14
# f-string method: string / 124 / 3.14
# f-string method: string / 125 / 3.14

 

 

판다스 10분 요약 : https://pandas.pydata.org/pandas-docs/stable/user_guide/10min.html

판다스 문서: https://pandas.pydata.org/pandas-docs/stable/user_guide/index.html

무료 전자책 참조: https://wikidocs.net/32829

import pandas as pd

df = pd.DataFrame(
    {'가격': [100, 140, 155, 70, 90],
     'PER': [1.1, 0.8, 0.7, 2.3, 3.9],
     '거래량': [1000, 800, 890, 700, 2000]},
    index=['a', 'b', 'c', 'd', 'e']
)

# 0. 기본데이터
print(df)
#     가격  PER   거래량
# a  100  1.1  1000
# b  140  0.8   800
# c  155  0.7   890
# d   70  2.3   700
# e   90  3.9  2000

# 1. 필터링
print(df[df['가격'] > 100])
#      가격  PER  거래량
# b  140  0.8  800
# c  155  0.7  890

# 조건이 여러개면 & 로직 연산자를 이용한다.!!! 
# df[df['가격'] > 100 & df['가격'] > 100]

# 필터링 데이터 구조.
print(type(df['가격'] > 100))
# <class 'pandas.core.series.Series'>

# 필터링 데이터 결과.
print((df['가격'] > 100))
# a    False
# b     True
# c     True
# d    False
# e    False
# Name: 가격, dtype: bool

# 2.1 시리즈 졍렬
print(df['PER'].sort_values(ascending=False))
# e    3.9
# d    2.3
# a    1.1
# b    0.8
# c    0.7
# Name: PER, dtype: float64

# 2.2 데이터프레임 정렬
print(df.sort_values(by='PER', ascending=True))
#     가격  PER   거래량
# c  155  0.7   890
# b  140  0.8   800
# a  100  1.1  1000
# d   70  2.3   700
# e   90  3.9  2000

# 3. rank. 시리즈로 결과 리턴
print(df['PER'].rank(ascending=True))
# a    3.0
# b    2.0
# c    1.0
# d    4.0
# e    5.0
# Name: PER, dtype: float64

+ Recent posts