Pandas DataFrame에서 n번째 행을 얻으려면 iloc()를 사용할 수 있습니다. 방법. 예:df.iloc[4] 행 번호가 0부터 시작하므로 5번째 행을 반환합니다.
단계
- 2차원, 크기 변경 가능, 잠재적으로 이질적인 테이블 형식 데이터, df를 만듭니다.
- 입력 DataFrame, df를 인쇄합니다.
- nth_row 변수를 초기화합니다.
- iloc() 메서드를 사용하여 n번째 행을 가져옵니다.
- 반환된 DataFrame을 인쇄합니다.
예시
import pandas as pd
df = pd.DataFrame(
dict(
name=['John', 'Jacob', 'Tom', 'Tim', 'Ally'],
marks=[89, 23, 100, 56, 90],
subjects=["Math", "Physics", "Chemistry", "Biology", "English"]
)
)
print "Input DataFrame is:\n", df
nth_row = 3
df = df.iloc[nth_row]
print "Row ", nth_row, "of the DataFrame is: \n", df 출력
Input DataFrame is:
name marks subjects
0 John 89 Math
1 Jacob 23 Physics
2 Tom 100 Chemistry
3 Tim 56 Biology
4 Ally 90 English
Row 3 of the DataFrame is:
name Tim
marks 56
subjects Biology
Name: 3, dtype: object