Pandas dataframe에서 셀 값을 얻는 방법


다음 구문을 사용하여 Pandas DataFrame에서 셀 값을 가져올 수 있습니다.

 #iloc method
df. iloc [0][' column_name ']

#atmethod
df. at [0, ' column_name ']

#values method
df[' column_name ']. values [0]

세 가지 메서드 모두 동일한 값을 반환합니다.

다음 예에서는 다음 pandas DataFrame에서 이러한 각 메서드를 사용하는 방법을 보여줍니다.

 import pandas as pd

#createDataFrame
df = pd. DataFrame ({' points ': [25, 12, 15, 14, 19, 23, 25, 29],
                   ' assists ': [5, 7, 7, 9, 12, 9, 9, 4],
                   ' rebounds ': [11, 8, 10, 6, 6, 5, 9, 12]})

#view DataFrame
df

        points assists rebounds
0 25 5 11
1 12 7 8
2 15 7 10
3 14 9 6
4 19 12 6
5 23 9 5
6 25 9 9
7 29 4 12

방법 1: iloc 함수를 사용하여 셀 값 가져오기

다음 코드는 .iloc 함수를 사용하여 pandas DataFrame에서 다양한 셀 값을 얻는 방법을 보여줍니다.

 #get value in first row in 'points' column
df. iloc [0][' points ']

25

#get value in second row in 'assists' column
df. iloc [1][' assists ']

7

방법 2: at 함수를 사용하여 셀 값 가져오기

다음 코드는 .at 함수를 사용하여 Pandas DataFrame에서 다양한 셀 값을 가져오는 방법을 보여줍니다.

 #get value in first row in 'points' column
df. at [0, ' points ']

25

#get value in second row in 'assists' column
df. at [1, ' assists ']

7

방법 3: 값 함수를 사용하여 셀 값 가져오기

다음 코드는 .values 함수를 사용하여 pandas DataFrame에서 다양한 셀 값을 얻는 방법을 보여줍니다.

 #get value in first row in 'points' column
df[' points ']. values [0] 

25

#get value in second row in 'assists' column
df[' assists ']. values [1] 

7

세 가지 메서드 모두 동일한 값을 반환합니다.

추가 리소스

Pandas 시리즈를 NumPy 배열로 변환하는 방법
Pandas DataFrame의 첫 번째 행을 얻는 방법
Pandas DataFrame에서 첫 번째 열을 얻는 방법

의견을 추가하다

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다