Pandas DataFrame.loc[] メソッド
Pandas DataFrame は、ラベル付きの軸 (行と列) を備えた 2 次元のサイズ可変の、潜在的に異種混合の表形式のデータ構造です。算術演算は行ラベルと列ラベルの両方に配置されます。これは、Series オブジェクトの dict のようなコンテナーと考えることができます。これは、 パンダ 。
Pandas DataFrame loc[] 構文
パンダ データフレーム.loc 属性は、ラベルまたは指定されたブール配列によって行と列のグループにアクセスします。 パンダのデータフレーム 。
構文: データフレーム.loc
パラメータ: なし
戻り値 : スカラー、シリーズ、データフレーム
Pandas DataFrame loc プロパティ
以下に、Pandas DataFrame loc[] を使用できる例をいくつか示します。
例 1: loc[] を使用してラベルによって単一の行と列を選択する
DataFrame.loc 属性を使用して、指定されたセル内の特定のセルにアクセスします。 パンダのデータフレーム インデックスと列ラベルを使用します。次に、loc[] を使用してラベルによって単一の行と列を選択します。
Python3
# 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 内の 2 つの列を返し、以下の例のように複数の行と列を選択します。
Python3
# 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: 2 つの行または列の間で選択する
この例では、「df」という名前のパンダ データフレームを作成し、カスタム行インデックスを設定してから、 loc> 「Row_2」から「Row_4」までの行と列「B」から「D」を選択するためのアクセサー。選択した行と列が印刷され、ラベルベースのインデックス作成の使用例が示されます。 loc> 。
Python3
# 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)> |
出力
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」という名前のパンダ データフレームを作成し、カスタム行インデックスを設定してから、 iloc> 代替行 (2 行ごと) と代替列 (2 列ごと) を選択するためのアクセサー。結果の選択結果が印刷され、整数ベースのインデックス付けの使用方法が示されます。 iloc> 。
Python3
# 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」という名前のパンダ データフレームを作成し、カスタム行インデックスを設定し、 loc> 条件に基づいて行を選択するアクセサー。これは、列「A」の値が 5 より大きい行を選択することと、列「B」が NULL ではない行を選択することを示しています。結果の選択結果が印刷され、条件付きフィルターの使用方法が示されます。 loc> 。
Python3
# 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