Pandas dataframe에서 마지막 행을 얻는 방법(예제 포함)
다음 방법을 사용하여 Pandas DataFrame의 마지막 행을 가져올 수 있습니다.
방법 1: 마지막 행 가져오기(Pandas 시리즈로)
last_row = df. iloc [-1]
방법 2: 마지막 행 가져오기(Pandas DataFrame으로)
last_row = df. iloc [-1:]
다음 예에서는 다음 pandas DataFrame에서 실제로 각 메서드를 사용하는 방법을 보여줍니다.
import pandas as pd #createDataFrame df = pd. DataFrame ({' assists ': [3, 4, 4, 5, 6, 7, 8, 12, 15, 11], ' rebounds ': [1, 3, 3, 5, 2, 2, 1, 1, 0, 14], ' points ': [20, 22, 24, 25, 20, 28, 15, 29, 11, 12]}) #view DataFrame print (df) assists rebound points 0 3 1 20 1 4 3 22 2 4 3 24 3 5 5 25 4 6 2 20 5 7 2 28 6 8 1 15 7 12 1 29 8 15 0 11 9 11 14 12
예시 1: 마지막 행 가져오기(Pandas 시리즈로)
다음 코드는 DataFrame의 마지막 행을 pandas 시리즈로 가져오는 방법을 보여줍니다.
#get last row in Data Frame as Series last_row = df. iloc [-1] #view last row print (last_row) assists 11 rebounds 14 points 12 Name: 9, dtype: int64
type() 함수를 사용하여 결과가 실제로 pandas 시리즈인지 확인할 수 있습니다.
#viewtype type (last_row) pandas.core.series.Series
그 결과 실제로 일련의 팬더가 탄생했습니다.
예 2: 마지막 행 가져오기(Pandas DataFrame으로)
다음 코드는 DataFrame의 마지막 행을 pandas DataFrame으로 가져오는 방법을 보여줍니다.
#get last row in Data Frame as DataFrame last_row = df. iloc [-1:] #view last row print (last_row) assists rebound points 9 11 14 12
type() 함수를 사용하여 결과가 실제로 pandas DataFrame인지 확인할 수 있습니다.
#viewtype type (last_row) pandas.core.frame.DataFrame
결과는 실제로 pandas DataFrame입니다.
추가 리소스
다음 튜토리얼에서는 Pandas에서 다른 일반적인 작업을 수행하는 방법을 설명합니다.
Pandas에서 NaN 값 없이 행을 선택하는 방법
Pandas에서 특정 행을 제외한 모든 행을 삭제하는 방법
Pandas에서 특정 열을 합산하는 방법