pandas 시작하기 === [TOC] ## 설치 일반적인 환경 > pip install pandas Anaconda 환경 > conda install pandas ## 데이터 프레임 ![](https://hackmd.io/_uploads/S10C50Vms.png) #### 데이터 프레임 생성 ```python! # 딕셔너리 유형으로 data를 만든 후 데이터 프레임 생성하기 import pandas as pd df = pd.DataFrame( { "Name": [ "Braund, Mr. Owen Harris", "Allen, Mr. William Henry", "Bonnell, Miss. Elizabeth", ], "Age": [22, 35, 58], "Sex": ["male", "male", "female"], } ) ``` ```python # 행 방향 인덱스는 0부터 시작 df Name Age Sex 0 Braund, Mr. Owen Harris 22 male 1 Allen, Mr. William Henry 35 male 2 Bonnell, Miss. Elizabeth 58 female ``` :::info DataFrame의 각 칼럼은 Series로 구성되어 있다. ::: #### 열 선택하기 ```python df["Age"] 0 22 1 35 2 58 Name: Age, dtype: int64 # 2개 이상의 열 선택도 가능 df[["Age","Name"]] Age Name 0 22 Braund, Mr. Owen Harris 1 35 Allen, Mr. William Henry 2 58 Bonnell, Miss. Elizabeth ``` #### 행 선택하기 ```python # 인덱스를 입력하면 행 선택 df[0:2] Name Age Sex 0 Braund, Mr. Owen Harris 22 male 1 Allen, Mr. William Henry 35 male # 행,열을 동시 입력시 (범위로도 가능) df.loc[1, "Age"] 35 # 인덱스로 조회 (범위로도 가능) df.iloc[1,1] 35 ``` ###### tags: `python` `pandas`