Pandas dataframe의 첫 번째 열을 얻는 방법(예제 포함)
다음 구문을 사용하여 Pandas DataFrame의 첫 번째 열을 가져올 수 있습니다.
df. iloc [:, 0]
결과는 일련의 팬더입니다. 결과를 pandas DataFrame으로 만들려면 다음 구문을 사용할 수 있습니다.
df. iloc [:, :1]
다음 예에서는 이 구문을 실제로 사용하는 방법을 보여줍니다.
예 1: Pandas DataFrame에서 첫 번째 열 가져오기 (시리즈 반환)
다음 코드는 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 print (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 #get first column first_col = df. iloc [:, 0] #view first column print (first_col) 0 25 1 12 2 15 3 14 4 19 5 23 6 25 7 29 Name: points, dtype: int64
결과는 일련의 팬더입니다.
#check type of first_col print ( type (first_col)) <class 'pandas.core.series.Series'>
예 2: Pandas DataFrame의 첫 번째 열 가져오기(DataFrame 반환)
다음 코드는 Pandas DataFrame의 첫 번째 열을 가져오고 그에 따라 DataFrame을 반환하는 방법을 보여줍니다.
#get first column (and return a DataFrame) first_col = df. iloc [:, :1] #view first column print (first_col) points 0 25 1 12 2 15 3 14 4 19 5 23 6 25 7 29 #check type of first_col print ( type (first_col)) <class 'pandas.core.frame.DataFrame'>
추가 리소스
Pandas DataFrame에 열을 추가하는 방법
Pandas DataFrame에 열을 삽입하는 방법
Pandas의 조건을 기반으로 새 열을 만드는 방법