본문 바로가기

<개발 공부>/[python]

[Python / 파이썬] <Pandas> DataFrame 값에 접근하기 loc/iloc

Pandas의 DataFrame은 행과 열로 이루어져 있다.

DataFrame의 값을 가져오거나 수정이 필요한 경우, 행/열 중 선택하여 접근하여야 하는 경우 아래와 같이 접근 할 수 있다.

 

  • 행에 접근하는 방법 : loc/iloc 사용
  • 열에 접근하는 방법 : dictionary와 동일하게 바로 접근

 

행에 접근하는 방법


import pandas as pd

titanic = pd.read_csv("경로")

titanic.loc[0] # 0의 이름을 가진 행 출력
titanic.iloc[0] # 0번째 순서의 행 출력
  • loc[행 이름[, 열 이름]]
  • iloc[행 index[, 열 index]]

loc/iloc 를 이용하여 행/열의 이름이나 index를 활용하여 값에 접근 할 수 있으며, 행에 접근할때는 열을 제외하고 입력 할 수 있다.

 

열에 접근하는 방법


import pandas as pd

titanic = pd.read_csv("경로")

titanic["Age"]
titanic.Age