패키지

개발자를 위한 Python 중급

Jasmin Ludolf

Senior Data Science Content Developer

모듈 = Python 파일

  • 모듈 = Python 파일

  • 누구나 Python 파일을 만들 수 있음

노트북에 표시된 코드 파일

개발자를 위한 Python 중급

패키지

  • 모듈 모음 = 패키지
    • 라이브러리라고도 함
  • 공개적으로 이용 가능하고 무료임
  • PyPI에서 다운로드됨
  • 다운로드 후 모듈처럼 가져와서 사용

큰 골판지 상자

1 https://pypi.org/
개발자를 위한 Python 중급

패키지 설치하기

  • 터미널 / 명령 프롬프트

    python3 -m pip install <package_name>
    
  • python3 - 터미널에서 Python 코드 실행

  • pip - 권장 설치 프로그램

코딩 터미널

개발자를 위한 Python 중급

패키지 설치하기

python3 -m pip install pandas

Pandas 로고

$$

  • 데이터 가공 및 분석에 활용하는 패키지
개발자를 위한 Python 중급

별칭으로 가져오기

# Import pandas
import pandas
  • 코드를 짧게 만들려면 별칭 사용
# Import pandas using an alias
import pandas as pd
개발자를 위한 Python 중급

DataFrame 만들기

# Sales dictionary
sales = {"user_id": ["KM37", "PR19", "YU88"],
         "order_value": [197.75, 208.21, 134.99]}

# Convert to a pandas DataFrame sales_df = pd.DataFrame(sales)
print(sales_df)
  user_id  order_value
0    KM37       197.75
1    PR19       208.21
2    YU88       134.99
개발자를 위한 Python 중급

CSV 파일 읽기

# Reading in a CSV file in our current directory
sales_df = pd.read_csv("sales.csv")

# Checking the data type print(type(sales_df))
pandas.core.frame.DataFrame
개발자를 위한 Python 중급

파일 미리보기

# DataFrame method to preview the first five rows
print(sales_df.head())
  user_id  order_value
0    KM37       197.75
1    PR19       208.21
2    YU88       134.99
3    NT43       153.54        
4    IW06       379.47
개발자를 위한 Python 중급

파일 정보 확인하기

# Checking the file info
print(sales_df.info())
RangeIndex: 3 entries, 0 to 2
Data columns (total 2 columns):
 #   Column       Non-Null Count  Dtype  
<hr />  ------       --------------  -----  
 0   user_id      3 non-null      object 
 1   order_value  3 non-null      float64
dtypes: float64(1), object(1)
memory usage: 180.0+ bytes
개발자를 위한 Python 중급

함수 vs. 메서드

# This is a built-in function
print(sum([1, 2 ,3, 4, 5]))
15
  • 함수 = 작업을 수행하는 코드
# This is a pandas function
sales_df = pd.DataFrame(sales)
  • .head()는 pandas DataFrame에서만 작동
# This is a method
print(sales_df.head())
  user_id  order_value
0    KM37       197.75
1    PR19       208.21
2    YU88       134.99
3    NT43       153.54        
4    IW06       379.47
  • 메서드 = 특정 데이터 유형에만 사용할 수 있는 함수
개발자를 위한 Python 중급

연습해 봅시다!

개발자를 위한 Python 중급

Preparing Video For Download...