Pandas DataFrame은 레이블이 지정된 축(행 및 열)이 있는 2차원 크기 변경 가능하고 잠재적으로 이질적인 표 형식 데이터 구조입니다. 산술 연산은 행 및 열 레이블 모두에 맞춰 정렬됩니다. Series 객체에 대한 딕셔너리 같은 컨테이너로 생각할 수 있습니다. 이것이 기본 데이터 구조이다. 팬더 .
Pandas DataFrame loc[] 구문
팬더 데이터프레임.loc 속성은 지정된 레이블 또는 부울 배열을 기준으로 행과 열 그룹에 액세스합니다. 팬더 데이터프레임 .
통사론: 데이터프레임.loc
매개변수: 없음
반품 : 스칼라, 시리즈, 데이터프레임
Pandas DataFrame loc 속성
다음은 Pandas DataFrame loc[]을 사용할 수 있는 몇 가지 예입니다.
예시 1: loc[]를 사용하여 레이블로 단일 행과 열을 선택합니다.
DataFrame.loc 속성을 사용하여 주어진 특정 셀에 액세스합니다. 팬더 데이터프레임 인덱스와 열 레이블을 사용합니다. 그런 다음 loc[]를 사용하여 레이블별로 단일 행과 열을 선택합니다.
파이썬3
# importing pandas as pd> import> pandas as pd> # Creating the DataFrame> df> => pd.DataFrame({> 'Weight'> : [> 45> ,> 88> ,> 56> ,> 15> ,> 71> ],> > 'Name'> : [> 'Sam'> ,> 'Andrea'> ,> 'Alex'> ,> 'Robin'> ,> 'Kia'> ],> > 'Age'> : [> 14> ,> 25> ,> 55> ,> 8> ,> 21> ]})> # Create the index> index_> => [> 'Row_1'> ,> 'Row_2'> ,> 'Row_3'> ,> 'Row_4'> ,> 'Row_5'> ]> # Set the index> df.index> => index_> # Print the DataFrame> print> (> 'Original DataFrame:'> )> print> (df)> # Corrected selection using loc for a specific cell> result> => df.loc[> 'Row_2'> ,> 'Name'> ]> # Print the result> print> (> '
Selected Value at Row_2, Column 'Name':'> )> print> (result)> |
>
내 컴퓨터 화면이 얼마나 큰데
>
산출
Original DataFrame: Weight Name Age Row_1 45 Sam 14 Row_2 88 Andrea 25 Row_3 56 Alex 55 Row_4 15 Robin 8 Row_5 71 Kia 21 Selected Value at Row_2, Column 'Name': Andrea>
예 2: 여러 행과 열 선택
DataFrame.loc 속성을 사용하여 지정된 Dataframe에서 두 개의 열을 반환한 다음 아래 예에서와 같이 여러 행과 열을 선택합니다.
파이썬3
# importing pandas as pd> import> pandas as pd> # Creating the DataFrame> df> => pd.DataFrame({> 'A'> :[> 12> ,> 4> ,> 5> ,> None> ,> 1> ],> > 'B'> :[> 7> ,> 2> ,> 54> ,> 3> ,> None> ],> > 'C'> :[> 20> ,> 16> ,> 11> ,> 3> ,> 8> ],> > 'D'> :[> 14> ,> 3> ,> None> ,> 2> ,> 6> ]})> # Create the index> index_> => [> 'Row_1'> ,> 'Row_2'> ,> 'Row_3'> ,> 'Row_4'> ,> 'Row_5'> ]> # Set the index> df.index> => index_> # Print the DataFrame> print> (> 'Original DataFrame:'> )> print> (df)> # Corrected column names ('A' and 'D') in the result> result> => df.loc[:, [> 'A'> ,> 'D'> ]]> # Print the result> print> (> '
Selected Columns 'A' and 'D':'> )> print> (result)> |
>
>
산출
Original DataFrame: A B C D Row_1 12.0 7 20 14.0 Row_2 4.0 2 16 3.0 Row_3 5.0 54 11 NaN Row_4 NaN 3 3 2.0 Row_5 1.0 NaN 8 6.0 Selected Columns 'A' and 'D': A D Row_1 12.0 14.0 Row_2 4.0 3.0 Row_3 5.0 NaN Row_4 NaN 2.0 Row_5 1.0 6.0>
예 3: 두 행 또는 열 중에서 선택
이 예에서는 'df'라는 팬더 DataFrame을 만들고 사용자 정의 행 인덱스를 설정한 다음loc>
접근자는 'Row_2'부터 'Row_4'까지의 행과 'B'부터 'D'까지의 열을 선택합니다. 선택한 행과 열이 인쇄되어 레이블 기반 인덱싱 사용을 보여줍니다.loc>
.
파이썬3
# importing pandas as pd> import> pandas as pd> # Creating the DataFrame> df> => pd.DataFrame({> 'A'> : [> 12> ,> 4> ,> 5> ,> None> ,> 1> ],> > 'B'> : [> 7> ,> 2> ,> 54> ,> 3> ,> None> ],> > 'C'> : [> 20> ,> 16> ,> 11> ,> 3> ,> 8> ],> > 'D'> : [> 14> ,> 3> ,> None> ,> 2> ,> 6> ]})> # Create the index> index_> => [> 'Row_1'> ,> 'Row_2'> ,> 'Row_3'> ,> 'Row_4'> ,> 'Row_5'> ]> # Set the index> df.index> => index_> # Print the original DataFrame> print> (> 'Original DataFrame:'> )> print> (df)> # Select Rows Between 'Row_2' and 'Row_4'> selected_rows> => df.loc[> 'Row_2'> :> 'Row_4'> ]> print> (> '
Selected Rows:'> )> print> (selected_rows)> # Select Columns 'B' through 'D'> selected_columns> => df.loc[:,> 'B'> :> 'D'> ]> print> (> '
Selected Columns:'> )> print> (selected_columns)> |
RDBMS 정규화
>
>
산출
Original DataFrame: A B C D Row_1 12.0 7 20 14.0 Row_2 4.0 2 16 3.0 Row_3 5.0 54 11 NaN Row_4 NaN 3 3 2.0 Row_5 1.0 NaN 8 6.0 Selected Rows: A B C D Row_2 4 2 16 3.0 Row_3 5 54 11 NaN Row_4 NaN 3 3 2.0 Selected Columns: B C D Row_1 7 20 14.0 Row_2 2 16 3.0 Row_3 54 11 NaN Row_4 3 3 2.0 Row_5 NaN 8 6.0>
예 4: 대체 행 또는 열 선택
이 예에서는 'df'라는 팬더 DataFrame을 만들고 사용자 정의 행 인덱스를 설정한 다음iloc>
대체 행(두 번째 행마다)과 대체 열(두 번째 열마다)을 선택하는 접근자입니다. 결과 선택 항목이 인쇄되어 정수 기반 인덱싱 사용을 보여줍니다.iloc>
.
파이썬3
# importing pandas as pd> import> pandas as pd> # Creating the DataFrame> df> => pd.DataFrame({> 'A'> : [> 12> ,> 4> ,> 5> ,> None> ,> 1> ],> > 'B'> : [> 7> ,> 2> ,> 54> ,> 3> ,> None> ],> > 'C'> : [> 20> ,> 16> ,> 11> ,> 3> ,> 8> ],> > 'D'> : [> 14> ,> 3> ,> None> ,> 2> ,> 6> ]})> # Create the index> index_> => [> 'Row_1'> ,> 'Row_2'> ,> 'Row_3'> ,> 'Row_4'> ,> 'Row_5'> ]> # Set the index> df.index> => index_> # Print the original DataFrame> print> (> 'Original DataFrame:'> )> print> (df)> # Select Alternate Rows> alternate_rows> => df.iloc[::> 2> ]> print> (> '
Alternate Rows:'> )> print> (alternate_rows)> # Select Alternate Columns> alternate_columns> => df.iloc[:, ::> 2> ]> print> (> '
Alternate Columns:'> )> print> (alternate_columns)> |
>
>
자바를 잡아서 시도해 보세요
산출
Original DataFrame: A B C D Row_1 12.0 7 20 14.0 Row_2 4.0 2 16 3.0 Row_3 5.0 54 11 NaN Row_4 NaN 3 3 2.0 Row_5 1.0 NaN 8 6.0 Alternate Rows: A B C D Row_1 12.0 7 20 14.0 Row_3 5.0 54 11 NaN Row_5 1.0 NaN 8 6.0 Alternate Columns: A C Row_1 12.0 20 Row_2 4.0 16 Row_3 5.0 11 Row_4 NaN 3 Row_5 1.0 8>
예 5: Pandas loc에 조건 사용
이 예에서는 'df'라는 pandas DataFrame을 만들고 사용자 정의 행 인덱스를 설정하고loc>
조건에 따라 행을 선택하는 접근자입니다. 'A' 열의 값이 5보다 큰 행을 선택하고 'B' 열이 null이 아닌 행을 선택하는 방법을 보여줍니다. 그런 다음 결과 선택 항목이 인쇄되어 조건부 필터링 사용을 보여줍니다.loc>
.
파이썬3
# importing pandas as pd> import> pandas as pd> # Creating the DataFrame> df> => pd.DataFrame({> 'A'> : [> 12> ,> 4> ,> 5> ,> None> ,> 1> ],> > 'B'> : [> 7> ,> 2> ,> 54> ,> 3> ,> None> ],> > 'C'> : [> 20> ,> 16> ,> 11> ,> 3> ,> 8> ],> > 'D'> : [> 14> ,> 3> ,> None> ,> 2> ,> 6> ]})> # Create the index> index_> => [> 'Row_1'> ,> 'Row_2'> ,> 'Row_3'> ,> 'Row_4'> ,> 'Row_5'> ]> # Set the index> df.index> => index_> # Print the original DataFrame> print> (> 'Original DataFrame:'> )> print> (df)> # Using Conditions with loc> # Example: Select rows where column 'A' is greater than 5> selected_rows> => df.loc[df[> 'A'> ]>> 5> ]> print> (> '
Rows where column 'A' is greater than 5:'> )> print> (selected_rows)> # Example: Select rows where column 'B' is not null> non_null_rows> => df.loc[df[> 'B'> ].notnull()]> print> (> '
Rows where column 'B' is not null:'> )> print> (non_null_rows)> |
>
>
산출
Original DataFrame: A B C D Row_1 12.0 7 20 14.0 Row_2 4.0 2 16 3.0 Row_3 5.0 54 11 NaN Row_4 NaN 3 3 2.0 Row_5 1.0 NaN 8 6.0 Rows where column 'A' is greater than 5: A B C D Row_1 12.0 7 20 14.0 Row_3 5.0 54 11 NaN Rows where column 'B' is not null: A B C D Row_1 12.0 7 20 14.0 Row_2 4.0 2 16 3.0 Row_3 5.0 54 11 NaN Row_4 NaN 3 3 2.0>